-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondensed_list.py
More file actions
51 lines (37 loc) · 978 Bytes
/
condensed_list.py
File metadata and controls
51 lines (37 loc) · 978 Bytes
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
def condense_list(head):
prev_node = head
curr_node = head
seen = set()
while curr_node:
if curr_node.data in seen:
# skip the seen node. update prev node's next to curr node's next
prev_node.next = curr_node.next
else:
seen.add(curr_node.data)
# update prev node to curr node
prev_node = curr_node
curr_node = curr_node.next
return prev_node
class Node:
def __init__(self, data):
self.data = data
self.next = None
def add_node(self, node):
self.next = node
if __name__ == '__main__':
def create_list():
a = Node(1)
b = Node(2)
c = Node(3)
d = Node(1)
e = Node(2)
a.add_node(b)
b.add_node(c)
c.add_node(d)
d.add_node(e)
return a
head = create_list()
condense_list(head)
while head:
print(head.data)
head = head.next