-
Notifications
You must be signed in to change notification settings - Fork 19
/
40_Dedup-Linked-List.py
58 lines (47 loc) · 1.16 KB
/
40_Dedup-Linked-List.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
#!/usr/bin/python
# coding=utf-8
'''
__author__ = 'sunp'
__date__ = '2019/1/28'
Given an unsorted linked list, write a function to remove all the duplicates.
dedup(1 -> 2 -> 3 -> 2 -> 1) = 1 -> 2 -> 3
'''
class LinkNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
def dedup1(head):
# hashset
unq = set()
res, pre = head, None
while head:
if head.val in unq:
pre.next = head.next
else:
unq.add(head.val)
pre = head
head = head.next
return res
def dedup2(head):
# brute
res = head
while head:
cur = head
while cur.next:
if cur.next.val == cur.val:
cur.next = cur.next.next
else:
cur = cur.next
head = head.next
return res
if __name__ == '__main__':
test = LinkNode(1)
test.next = LinkNode(2)
test.next.next = LinkNode(3)
test.next.next.next = LinkNode(2)
test.next.next.next.next = LinkNode(1)
for dedup in [dedup1, dedup2]:
node = dedup(test)
while node:
print(node.val)
node = node.next