-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path06_singlyLL_remove_dup.py
69 lines (55 loc) · 1.48 KB
/
06_singlyLL_remove_dup.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def prepend(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
cur_node = self.head
self.head = new_node
new_node.next = cur_node
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def remove_duplicates(self):
cur_node = self.head
prev = None
dup_values = dict()
while cur_node:
if cur_node.data in dup_values:
# Remove node:
prev.next = cur_node.next
cur_node = None
else:
# Have not encountered element before
dup_values[cur_node.data] = 1
prev = cur_node
cur_node = prev.next
llist = SinglyLinkedList()
llist.append(1)
llist.append(2)
llist.append(7)
llist.append(2)
llist.append(1)
llist.append(7)
llist.append(1)
llist.print_list()
print("\n")
llist.remove_duplicates()
llist.print_list()