-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremove_dups.py
59 lines (53 loc) · 1.52 KB
/
remove_dups.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def printLL(self):
temp = self.head
while(temp):
print(temp.data, end = ' ')
temp = temp.next
#O(n^2)
def remove_duplicates_simple(self):
current = self.head
if current is None: return
prev = None
while(current is not None):
prev = current
while(prev.next is not None):
if(prev.next.data == current.data):
prev.next = prev.next.next
else:
prev = prev.next
current = current.next
def remove_duplicates_hashed(self):
current = self.head
if current is None: return
cnt_int = set()
prev = None
while(current is not None):
if current.data in cnt_int:
prev.next = current.next
else:
cnt_int.add(current.data)
prev = current
current = current.next
if __name__ == "__main__":
ll = LinkedList()
inp = list(map(int, input().split()))
for i in inp:
ll.push(i)
ll.printLL()
print()
ll.remove_duplicates_simple()
ll.printLL()
print()
ll.remove_duplicates_hashed()
ll.printLL()