[LeetCode] 138. Copy List with Random Pointer

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

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.

Return the head of the copied linked list.

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

  • val: an integer representing Node.val
  • random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.

Your code will only be given the head of the original linked list.

Example 1:

Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]

Example 2:

Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]

Example 3:

Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]

Constraints:

  • 0 <= n <= 1000
  • -104 <= Node.val <= 104
  • Node.random is null or is pointing to some node in the linked list.

复制带随机指针的链表。

给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。

例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。

返回复制链表的头节点。

用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:

val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。
你的代码 只 接受原链表的头节点 head 作为传入参数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/copy-list-with-random-pointer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给了一个带有 next 和 random 两个指针的单链表,对其进行深度复制(deep copy)。例子,

Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]

Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]

明确一下什么叫做深度复制

这样叫做 shallow copy 浅复制,a 和 b 在内存中是 share 一个地址。

ListNode a = new ListNode(0);

ListNode b = a;

这样叫做深度复制,a 和 b 在内存中各自有各自的地址。

ListNode a = new ListNode(0);

ListNode b = new ListNode(0);

这道题有两种思路,一是用 hashmap,key 是每个 node,value 是存每个 node 的 copy。复制完毕之后再遍历一遍,做每个 node 的指针复制。

map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);

时间O(n)

空间O(n)

JavaScript实现

 1 /**
 2  * @param {Node} head
 3  * @return {Node}
 4  */
 5 var copyRandomList = function(head) {
 6     if (head === null) return null;
 7     let map = new Map();
 8     let cur = head;
 9     while (cur !== null) {
10         map.set(cur, new Node(cur.val, cur.next, cur.random));
11         cur = cur.next;
12     }
13     cur = head;
14     while (cur !== null) {
15         map.get(cur).next = map.get(cur.next) || null;
16         map.get(cur).random = map.get(cur.random) || null;
17         cur = cur.next;
18     }
19     return map.get(head);
20 };

 

Java实现

 1 class Solution {
 2     public Node copyRandomList(Node head) {
 3         HashMap<Node, Node> map = new HashMap<>();
 4         Node cur = head;
 5         while (cur != null) {
 6             map.put(cur, new Node(cur.val));
 7             cur = cur.next;
 8         }
 9         cur = head;
10         while (cur != null) {
11             map.get(cur).next = map.get(cur.next);
12             map.get(cur).random = map.get(cur.random);
13             cur = cur.next;
14         }
15         return map.get(head);
16     }
17 }

 

另外一种思路是首先复制整个链表,将每一个复制的节点 copy 加到原节点后面。然后将每个 copy node 的 random node 也链接好,再断开原链表和复制的链表。这种做法无需额外空间。

注意因为 random node 其实也是来自于这个链表,跟 next 指针的区别在于 next 指针是指向每个 node 的下一个 node,random 指针是随机指向了整个链表中的某个 node,所以当第一遍复制好了 copy node 之后,也需要把 cur.next.random 指向 cur.random.next,因为

  • cur.next.random = copy.random
  • cur.random.next = 某一个copy node

时间O(n)

空间O(1)

JavaScript实现

 1 /**
 2  * @param {Node} head
 3  * @return {Node}
 4  */
 5 var copyRandomList = function (head) {
 6     // make copy of each node
 7     let cur = head;
 8     while (cur) {
 9         let next = cur.next;
10         let copy = new Node(cur.val);
11         cur.next = copy;
12         copy.next = next;
13         cur = next;
14     }
15 
16     // set random property
17     cur = head;
18     while (cur) {
19         if (cur.random !== null) {
20             cur.next.random = cur.random.next;
21         }
22         cur = cur.next.next;
23     }
24 
25     // detach copied list
26     cur = head;
27     let dummyHead = new Node(0);
28     let newHead = dummyHead;
29     while (cur) {
30         let next = cur.next.next;
31         // extract the copy
32         let copy = cur.next;
33         newHead.next = copy;
34         newHead = copy;
35         // restore the original list
36         cur.next = next;
37         cur = next;
38     }
39     return dummyHead.next;
40 };

 

Java实现

 1 class Solution {
 2     public Node copyRandomList(Node head) {
 3         // corner case
 4         if (head == null) {
 5             return null;
 6         }
 7 
 8         // copy all the cur nodes
 9         Node cur = head;
10         while (cur != null) {
11             Node next = cur.next;
12             Node copy = new Node(cur.val);
13             cur.next = copy;
14             copy.next = next;
15             cur = next;
16         }
17 
18         // copy all the random nodes
19         cur = head;
20         while (cur != null) {
21             if (cur.random != null) {
22                 // cur.random.next是某个random节点的copy
23                 // cur.next.random是当前节点的copy的random指针
24                 cur.next.random = cur.random.next;
25             }
26             cur = cur.next.next;
27         }
28 
29         // separate two lists
30         cur = head;
31         Node dummy = new Node(0);
32         Node copyHead = dummy;
33         Node copy;
34         while (cur != null) {
35             Node next = cur.next.next;
36             copy = cur.next;
37             copyHead.next = copy;
38             copyHead = copy;
39             cur.next = next;
40             cur = next;
41         }
42         return dummy.next;
43     }
44 }

 

最后附上discussion里面一个非常好的图解

 

相关题目

133. Clone Graph

138. Copy List with Random Pointer

1485. Clone Binary Tree With Random Pointer

1490. Clone N-ary Tree

LeetCode 题目总结

posted @ 2020-01-18 08:09  CNoodle  阅读(491)  评论(0编辑  收藏  举报