forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_duplicates.py
43 lines (38 loc) · 1.1 KB
/
delete_duplicates.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
from shared.linked_list import LinkedList
from shared.node import Node
def delete_duplicates(linked_list: LinkedList):
"""
Delete duplicate node from a Linked List
Parameters:
linked_list: The list to be deleted from
"""
current = linked_list.node
if current is None:
return
record = set()
record.add(current.data)
while current.next is not None:
# Duplicate if data is present in [record]
if current.next.data in record:
current.next = current.next.next
else:
record.add(current.next.data)
current = current.next
if __name__ == "__main__":
linked = LinkedList()
linked.add(Node(1))
linked.add(Node(2))
linked.add(Node(2))
linked.add(Node(2))
linked.add(Node(3))
linked.add(Node(4))
linked.add(Node(5))
linked.add(Node(1))
linked.add(Node(6))
linked.add(Node(7))
delete_duplicates(linked)
linked.print()
assert len(linked) == 7
# Linked list must contain 1 to 7 in order
for [node, num] in zip(linked, range(1, 8)):
assert node.data == num