简繁EN

LeetCodeNo. 130

[LeetCode with Python] 138. Copy List with Random Pointer

Contents

Problem

Problem page: https://leetcode.com/problems/copy-list-with-random-pointer/
Topics: Hash Table, Linked List
Difficulty: Hard
Similar problems: (M) Clone Graph

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.


Analysis

The tricky part is random. It's easy to copy a linked list by following next, but to make each new node's random point to the copy of the original node's random target, I need a way to find the copy of a given original node. I temporarily use the original node's next and the copy's random to keep track of the pair.
I save the original nodes in order in save_list, then follow next to make a new linked list, copying each node's label (its value) and leaving random alone. I walk both lists together, setting each original node's next to point to its copy and the copy's random to point back to the original node.
Next I walk the new list once, setting each copy's random to copy.random.random.next. The three steps take me to the original node, its original target, and the copy of that target. If the original node's random is null, I set the copy's random to null.
I need to fill save_list ahead of time and wait until I've set all the random pointers before using it to restore each original node's next to its original successor. If I restore each original node's next as I go through the loop that sets random, I'll get the wrong node when random points to an earlier node. I've already restored that node's next, so following next no longer gives me its copy.
The time complexity is O(n), and the extra space is O(n) too, all of it for save_list.


Code

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
# Definition for singly-linked list with a random pointer.
class RandomListNode:
def __init__(self, x):
self.label = x
self.next = None
self.random = None

class Solution:
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
if None == head:
return None
save_list = [ ]
p1 = head
while None != p1:
save_list.append(p1)
p1 = p1.next
new_head = RandomListNode(-1)
new_head.next = head
first = new_head
second = head
copy_head = RandomListNode(-1)
copy_first = copy_head
copy_second = None
while None != first:
copy_second = RandomListNode(second.label) if None != second else None
copy_first.next = copy_second
copy_first = copy_first.next
first = first.next
if None != second:
second = second.next

p1 = head
p1_tail = head.next
p2 = copy_head.next
while None != p1:
p1_tail = p1.next
p1.next = p2
p2.random = p1
p1 = p1_tail
p2 = p2.next
p2 = copy_head.next
#p1 = head
while None != p2:
p2.random = p2.random.random.next if None != p2.random.random else None
#p1.next = p1.next.next.random if None != p1.next.next else None # may broken the previous p1.next, so have to save ori list
p2 = p2.next
#p1 = p1.next
len_save_list = len(save_list)
for i in range(0, len_save_list - 1):
save_list[i].next = save_list[i + 1]
save_list[len_save_list - 1].next = None
return copy_head.next

Article link: https://xnerv.wang/en/leetcode-copy-list-with-random-pointer/