Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

138. Copy List with Random Pointer

Posted on 2020-05-05 | Edited on 2021-01-22

https://leetcode.com/problems/copy-list-with-random-pointer/

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 {
int val;
Node next;
Node random;

public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}*/
// My solution: two passes + recursion
/*
class Solution {
Map<Node,Node> map=new HashMap<>();
public Node copyRandomList(Node head) {
Node newHead=copyRandomListUtil(head);
Node dummy=newHead;
while(head!=null){
if(head.random!=null) newHead.random=map.get(head.random);
head=head.next;
newHead=newHead.next;
}
return dummy;
}
public Node copyRandomListUtil(Node head) {
if(head==null) return head;
Node cloneNode=new Node(head.val);
map.put(head,cloneNode);
cloneNode.next=copyRandomListUtil(head.next);
return cloneNode;
}
}*/

// Solution from leetcode: recursion + one pass
// Time Complexity : O(N), Space Complexity : O(N)
class Solution {
Map<Node,Node> map=new HashMap<>();
public Node copyRandomList(Node head) {
if(head==null) return head;
if(map.containsKey(head)) return map.get(head);
Node cloneNode=new Node(head.val);
map.put(head,cloneNode);
cloneNode.next=copyRandomList(head.next);
cloneNode.random=copyRandomList(head.random);
return cloneNode;
}
}

Same idea as above using HashMap link-solution)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution{
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) return null;

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

// loop 1. copy all the nodes
RandomListNode node = head;
while (node != null) {
map.put(node, new RandomListNode(node.label));
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);
}
}

Time Complexity : O(N), Space Complexity : O(1) link

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
public class lc138 {
public Node copyRandomList(Node head) {
Node iter = head, next;
// First round: make copy of each node,
// and link them together side-by-side in a single list.
while (iter != null) {
next = iter.next;

Node copy = new Node(iter.val);
iter.next = copy;
copy.next = next;

iter = next;
}

// Second round: assign random pointers for the copy nodes.
iter = head;
while (iter != null) {
if (iter.random != null) {
iter.next.random = iter.random.next;
}
iter = iter.next.next;
}

// Third round: restore the original list, and extract the copy list.
iter = head;
Node pseudoHead = new Node(0);
Node copy, copyIter = pseudoHead;
while (iter != null) {
next = iter.next.next;
// extract the copy
copy = iter.next;
copyIter.next = copy;
copyIter = copy;
// restore the original list
iter.next = next;
iter = next;
}
return pseudoHead.next;
}
}

<1…230231232…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%