-
Notifications
You must be signed in to change notification settings - Fork 7
/
copy_list_random_pointer.py
48 lines (40 loc) · 1.48 KB
/
copy_list_random_pointer.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
"""
# Definition for a Node.
class Node:
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
"""
class Solution(object): # 48ms
maps = {}
def copyRandomList(self, head: 'Node') -> 'Node':
if head == None:
return None
# tmp = head
# while tmp != None:
# print(tmp, "next-> ", tmp.next if tmp.next else None, ", random ->", tmp.random if tmp.random else None, "\n")
# tmp = tmp.next
head_copy = Node(head.val, head.next, head.random)
cur = head_copy.next
cur_parent = head_copy
self.maps[head] = head_copy
to_process = []
if head.random != None:
to_process.append(head_copy)
while cur != None:
tmp = Node(cur.val, cur.next, cur.random)
self.maps[cur] = tmp
cur_parent.next = tmp
if cur.random != None:
to_process.append(tmp)
cur_parent = tmp
cur = cur.next
for node_idx in range(len(to_process)):
to_process[node_idx].random = self.maps[to_process[node_idx].random]
# print("\n\n NewList\n")
# tmp = head_copy
# while tmp != None:
# print(tmp, "next-> ", tmp.next if tmp.next else None, ", random ->", tmp.random if tmp.random else None, "\n")
# tmp = tmp.next
return head_copy