-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse_Linked_List.py
52 lines (40 loc) · 988 Bytes
/
Reverse_Linked_List.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
52
class Node:
def __init__(self, value):
self.data = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
self.n = 0
def __len__(self):
return self.n
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
self.n = self.n + 1
def printList(self):
curr = self.head
while curr != None:
print(str(curr.data)+"->", end='')
curr = curr.next
print('NULL')
def reverse(self):
curr = self.head
self.head = self.tail
self.tail = self.head
after = curr.next
before = None
for _ in range(len(L)):
after = curr.next
curr.next = before
before = curr
curr = after
L = LinkedList()
L.push(3)
L.push(4)
L.push(1)
L.printList()
L.reverse()
L.printList()