forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyRandomList.py
51 lines (43 loc) · 1.24 KB
/
copyRandomList.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
# method 1: using a dict and store each node as key, each copied node as value
# Space complexity: O(n)
# Time complexity: O(n)
def copyRandomList(self, head):
d = {}
m = n = head
while m:
d[m] = RandomListNode(m.label)
m = m.next
while n:
d[n].next = d.get(n.next)
d[n].random = d.get(n.random)
n = n.next
return d.get(head)
# Method 2:
# Space complexity: O(1)
# Time complexity: O(n)
def copyRandomList(self, head):
if not head: return None
# Step 1: copy each node and link them together with original ones
curr = head
while curr:
new = RandomListNode(curr.label)
new.next = curr.next
curr.next = new
curr = curr.next.next
# Step 2: build random node for each copied nodes
curr = head
while curr:
if curr.random: curr.next.random = curr.random.next
curr = curr.next.next
# Step 3: restore the original list and extract copied list
newhead = head.next
pold = head
pnew = newhead
while pnew.next:
pold.next = pnew.next
pold = pold.next
pnew.next = pold.next
pnew = pnew.next
pold.next = None
pnew.next = None
return newhead