forked from siddharthpathak/python-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.py
51 lines (34 loc) · 1.03 KB
/
linkedList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
__author__ = 'siddharth'
import copy
class Node:
def __init__(self,data):
self.data = data
self.next = None
def display(self):
current = self
while current is not None:
print current.data,
current = current.next
def insert(self, data):
current = self
while current.next is not None:
current = current.next
current.next = Node(data)
def reverse(self):
current = copy.deepcopy(self)
prev = None
while current.next is not None:
next = current.next
current.next = prev
prev = current
current = next
self.data = current.data
self.next = prev
N = int(raw_input("Enter Number of elements: "))
head = Node(int(raw_input("Enter Elements: ")))
for i in range(1,N):
head.insert(int(raw_input()))
if __name__ == "__main__":
head.display()
head.reverse()
head.display()