0%

Leetcode138-copyListWithRandomPointer

Description

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.

Example


1
2
3
4
5
6
Input:
{"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}

Explanation:
Node 1's value is 1, both of its next and random pointer points to Node 2.
Node 2's value is 2, its next pointer points to null and its random pointer points to itself.

Solution

Solution 1: O(1) space

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
/*
// Definition for a Node.
class Node {
public int val;
public Node next;
public Node random;

public Node() {}

public Node(int _val,Node _next,Node _random) {
val = _val;
next = _next;
random = _random;
}
};
*/
class Solution {
public Node copyRandomList(Node head) {
if (head == null) return null;
// loop 1: copy all nodes
Node cur = head;
while(cur != null){
Node next = cur.next;
cur.next = new Node(cur.val, next, null);
cur = next;
}

// loop 2: assign all random pointers
cur = head;
while(cur != null){
if (cur.random != null){
cur.next.random = cur.random.next;
}
cur = cur.next.next;
}

// loop 3: reconstrcut new list
cur = head;
Node newHead = cur.next;
while(cur != null){
Node next = cur.next.next;
Node copy = cur.next;
cur.next = next;
if (next != null)
copy.next = next.next;
cur = next;
}
return newHead;
}
}

Solution 2: HashMap, O(N) space
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
/*
// Definition for a Node.
class Node {
public int val;
public Node next;
public Node random;

public Node() {}

public Node(int _val,Node _next,Node _random) {
val = _val;
next = _next;
random = _random;
}
};
*/
class Solution {
public Node copyRandomList(Node head) {
if (head == null) return null;

Map<Node, Node> map = new HashMap<>();

// loop 1. copy all the nodes
Node node = head;
while (node != null) {
map.put(node, new Node(node.val, null, null));
node = node.next;
}

// loop 2. assign next and random pointers
node = head;
while (node != null) {
map.get(node).next = map.get(node.next);
map.get(node).random = map.get(node.random);
node = node.next;
}

return map.get(head);
}
}