-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q01_remove_dups.py
57 lines (44 loc) · 1.38 KB
/
Q01_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
import unittest
class Node:
def __init__(self, val, next):
self.val = val
self.next = next
def remove_dups(head):
if head is None:
return head
current = head
seen = set([current.val])
while current.next:
if current.next.val in seen:
current.next = current.next.next
else:
seen.add(current.next.val)
current = current.next
return head
def remove_dups_followup(head):
if head is None:
return head
current = head
while current:
runner = current
while runner.next:
if runner.next.val == current.val:
runner.next = runner.next.next
else:
runner = runner.next
current = current.next
return head
class Test(unittest.TestCase):
def test_remove_dups(self):
head = Node(1, Node(2, Node(1, Node(5, None))))
remove_dups(head)
self.assertEqual(head.val, 1)
self.assertEqual(head.next.val, 2)
self.assertEqual(head.next.next.val, 5)
head = Node(1, Node(2, Node(1, Node(5, None))))
remove_dups_followup(head)
self.assertEqual(head.val, 1)
self.assertEqual(head.next.val, 2)
self.assertEqual(head.next.next.val, 5)
self.assertEqual(None, remove_dups(None))
self.assertEqual(None, remove_dups_followup(None))