-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.1 Remove Duplicates.py
62 lines (50 loc) · 1.54 KB
/
2.1 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
from sets import Set
from single_linked_list import Node, LinkedList
def remove_duplicates(self):
if self.head.next == None or self.head.next.next == None:
return
before = self.head
current = before.next
unique = Set([])
while current:
if current.val in unique:
before.next = current.next
else:
unique.add(current.val)
before = current
current = current.next
setattr(LinkedList, 'remove_duplicates', remove_duplicates)
val_list = [1, 2, 3, 2, 1, 1, 4]
linked_list = LinkedList.from_val_list(val_list)
print linked_list
linked_list.remove_duplicates()
print linked_list
def is_head_unique(self):
if self == None or self.next == None:
return True
current = self
val = current.val
current = current.next
while current:
if current.val == val:
return False
current = current.next
return True
setattr(Node, 'is_head_unique', is_head_unique)
def remove_duplicates_no_buffer(self):
if self.head.next == None or self.head.next.next == None:
return
before = self.head
current = before.next
while current:
if current.is_head_unique() == False:
before.next = current.next
else:
before = current
current = current.next
setattr(LinkedList, 'remove_duplicates_no_buffer', remove_duplicates_no_buffer)
val_list = [1, 2, 3, 2, 1, 1, 4]
linked_list = LinkedList.from_val_list(val_list)
print linked_list
linked_list.remove_duplicates_no_buffer()
print linked_list