-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_131.py
76 lines (63 loc) · 1.34 KB
/
problem_131.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
class Node:
def __init__(self, val):
self.val = val
self.next = None
self.rand = None
def __repr__(self):
return self.val
def deep_clone(source):
node_to_index = dict()
node_index_to_rand_index = dict()
curr = source
i = 0
while curr:
node_to_index[curr] = i
curr = curr.next
i += 1
curr = source
i = 0
while curr:
rand_index = node_to_index[curr.rand]
node_index_to_rand_index[i] = rand_index
curr = curr.next
i += 1
dummy_head = Node('0')
tail = dummy_head
curr = source
index_to_node = dict()
i = 0
while curr:
new_node = Node(curr.val)
index_to_node[i] = new_node
curr = curr.next
i += 1
tail.next = new_node
tail = new_node
curr = dummy_head.next
i = 0
while curr:
rand_index = node_index_to_rand_index[i]
curr.rand = index_to_node[rand_index]
curr = curr.next
i += 1
return dummy_head.next
# Tests
a = Node('a')
b = Node('b')
a.next = b
c = Node('c')
b.next = c
d = Node('d')
c.next = d
e = Node('e')
d.next = e
a.rand = a
b.rand = a
c.rand = e
d.rand = c
e.rand = c
cloned = deep_clone(a)
assert cloned.val == 'a'
assert cloned.rand.val == 'a'
assert cloned.next.val == 'b'
assert cloned.next.rand.val == 'a'