-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathremove_duplicates_doubly.py
83 lines (69 loc) · 2.29 KB
/
remove_duplicates_doubly.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
76
77
78
79
80
81
82
83
""" 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 remove_duplicates(self):
""" removes duplicates from list """
duplicates = set()
current = self.head
while current is not None:
if current.data not in duplicates:
duplicates.add(current.data)
current = current.next
else:
previous_node = current.previous
del_node = current
current = current.next
previous_node.next = current
if current is not None:
current.previous = previous_node
del_node.previous = None
del_node.next = None
del del_node
def main():
""" operational function """
dll = DoublyLinkedList()
dll.insert_tail(4)
dll.insert_tail(2)
dll.insert_tail(2)
dll.insert_tail(4)
dll.insert_tail(1)
dll.insert_tail(6)
dll.insert_tail(1)
dll.print()
dll.remove_duplicates()
dll.print()
if __name__ == '__main__':
main()