-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove duplicates.py
64 lines (55 loc) · 1.33 KB
/
remove 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def printList(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
def append(self, data):
if not self.head:
self.head = Node(data)
return
new_node = Node(data)
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
def prepend(self, data):
if not self.head:
self.head = Node(data)
return
else:
new_node = Node(data)
new_node.next = self.head
self.head = new_node
return
def duplicate(self):
prev = None
curr = self.head
dup_value = dict()
while curr:
if curr.data in dup_value:
prev.next = curr.next
curr = None
else:
dup_value[curr.data] = 1
prev = curr
curr = prev.next
l1 = LinkedList()
l1.append(10)
l1.append(20)
l1.append(10)
l1.prepend(100)
l1.prepend(20)
l1.prepend(30)
l1.prepend(10)
print("before removing duplicates")
l1.printList()
l1.duplicate()
print("after removing duplicates")
l1.printList()