-
Notifications
You must be signed in to change notification settings - Fork 66
/
reverse_doubly_list.py
75 lines (61 loc) · 1.95 KB
/
reverse_doubly_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
""" Reverse Doubly linked list
URL: https://www.geeksforgeeks.org/reverse-a-doubly-linked-list/
"""
class Node:
""" Node class contains everything related to linked list node """
def __init__(self, data):
""" initializing single node with data """
self.data = data
self.next = None
self.previous = None
class DoublyLinkedList:
""" A Doubly linked list (DLL) contains an extra pointer, typically
called previous pointer, together with next pointer and data which
are there in singly linked list.
"""
def __init__(self):
""" initializing doublt linked list with zero node """
self.head = None
def insert_tail(self, data):
""" inserts node at the end of doubly linked list """
if self.head is None:
self.head = Node(data)
return
current = self.head
new_node = Node(data)
while current.next is not None:
current = current.next
current.next = new_node
new_node.previous = current
def print(self):
""" prints entire linked list without changing underlying data """
current = self.head
while current is not None:
print(" <->", current.data, end="")
current = current.next
print()
def reverse(self):
""" reverse doubly linked list """
temp = None
current = self.head
while current is not None:
temp = current.previous
current.previous = current.next
current.next = temp
current = current.previous
if temp is not None:
self.head = temp.previous
def main():
""" operational function """
dll = DoublyLinkedList()
dll.insert_tail(1)
dll.insert_tail(2)
dll.insert_tail(3)
dll.insert_tail(4)
dll.insert_tail(5)
dll.insert_tail(6)
dll.print()
dll.reverse()
dll.print()
if __name__ == '__main__':
main()