-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path131_Snapchat_Clone_LinkedList_with_random_pointers.py
executable file
·80 lines (55 loc) · 2.3 KB
/
131_Snapchat_Clone_LinkedList_with_random_pointers.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
70
71
72
73
74
75
76
77
78
79
80
"""
This question was asked by Snapchat.
Given the head to a singly linked list, where each node also has a “random” pointer that points to anywhere
in the linked list, deep clone the list.
"""
class RandPtrNode:
def __init__(self, data, nxt=None):
self.data = data
self.nxt = nxt
self.rand_ptr = None
def __repr__(self):
return "{}->{}".format(self.data, self.nxt)
def deep_copy(ll:RandPtrNode):
# contains key: original_node
# value: new_deep_copied_node
equivalent_node_dict = {}
iter_a = ll # iterator on the original linked list
ll_copy = RandPtrNode(iter_a.data) # create first node of the deep copied linked list
iter_b = ll_copy # iterator on the new linked list
# add ll_copy's first node as equivalent of the original first node
equivalent_node_dict[ll] = ll_copy
iter_a = iter_a.nxt
while iter_a:
new_copied_node = RandPtrNode(iter_a.data) # create new node
# add new_copied_node as equivalent to the node currently pointed by iter_a
equivalent_node_dict[iter_a] = new_copied_node
# set next node to new_copied_node of the node currently pointed to by iter_b
iter_b.nxt = new_copied_node
# move iter_b to new_copied_node
iter_b = new_copied_node
iter_a = iter_a.nxt
# reset iter pointers
iter_a = ll
iter_b = ll_copy
# now set the rand pointer of the copied list by looking up
# equivalent nodes in the dict
while iter_a:
if iter_a.rand_ptr:
iter_b.rand_ptr = equivalent_node_dict[iter_a.rand_ptr]
iter_a = iter_a.nxt
iter_b = iter_b.nxt
return ll_copy
if __name__ == '__main__':
ll_with_rand = RandPtrNode(1, nxt=RandPtrNode(2, nxt=RandPtrNode(3)))
ll_with_rand.rand_ptr = ll_with_rand # 1 points to itself
ll_with_rand.nxt.rand_ptr = ll_with_rand # 2 points to 1
ll_with_rand.nxt.nxt.rand_ptr = ll_with_rand # 3 points to 1
ll_with_rand_copy = deep_copy(ll_with_rand)
# tests
# print(ll_with_rand_copy.rand_ptr)
# print(ll_with_rand_copy.nxt.rand_ptr)
# print(ll_with_rand_copy.nxt.nxt.rand_ptr)
assert ll_with_rand_copy.rand_ptr.data == 1
assert ll_with_rand_copy.nxt.rand_ptr.data == 1
assert ll_with_rand_copy.nxt.nxt.rand_ptr.data == 1