[LeetCode] Copy List with Random Pointer

简介: Well, since we need to make a deep copy of the list and nodes in the list have a random pointer that may point to any node in the list (or NULL), we n...

Well, since we need to make a deep copy of the list and nodes in the list have a random pointer that may point to any node in the list (or NULL), we need to maintain a mapping from each node in the origianl list to the node in the copy list. If the copy of a node already exists, just use that copy without copying it again; otherwise, create a new copy and add it to the mapping.

The following code is pretty straight-forward.

 1 class Solution {
 2 public:
 3     RandomListNode *copyRandomList(RandomListNode *head) {
 4         if (!head) return NULL;
 5         mp[head] = new RandomListNode(head -> label);
 6         RandomListNode* run = head;
 7         RandomListNode* temp = mp[head];
 8         while (run) {
 9             /* Process the next pointer */
10             if (run -> next) {
11                 if (mp.find(run -> next) == mp.end())
12                     mp[run -> next] = new RandomListNode(run -> next -> label);
13                 temp -> next = mp[run -> next];
14             }
15             /* Process the random pointer */
16             if (run -> random) {
17                 if (mp.find(run -> random) == mp.end())
18                     mp[run -> random] =  new RandomListNode(run -> random -> label);
19                 temp -> random = mp[run -> random];
20             }
21             run = run -> next;
22             temp = temp -> next;
23         }
24         return mp[head];
25     }
26 private:
27     unordered_map<RandomListNode*, RandomListNode*> mp;
28 };

This code also has a very concise and easy-to-understand recursive solution as follows.

 1 class Solution {
 2 public:
 3     RandomListNode *copyRandomList(RandomListNode *head) {
 4         if (!head) return NULL;
 5         if (mp.find(head) != mp.end())
 6             return mp[head];
 7         RandomListNode* node = new RandomListNode(head -> label);
 8         mp[head] = node;
 9         node -> next = copyRandomList(head -> next);
10         node -> random = copyRandomList(head -> random);
11         return mp[head];
12     }
13 private:
14     unordered_map<RandomListNode*, RandomListNode*> mp;
15 };

This link has a clever solution using O(1) extra space, which is rewritten below in C++.

 1 class Solution {
 2 public:
 3     RandomListNode *copyRandomList(RandomListNode *head) {
 4         if (!head) return NULL;
 5         RandomListNode* run = head;
 6         /* Insert the copy of each node after it. */
 7         while (run) {
 8             RandomListNode* copy = new RandomListNode(run -> label);
 9             copy -> next = run -> next;
10             run -> next = copy;
11             run = run -> next -> next;
12         }
13         /* Set the random pointer for each copy. */
14         run = head;
15         while (run) {
16             if (run -> random)
17                 run -> next -> random = run -> random -> next;
18             run = run -> next -> next;
19         }
20         /* Extract the copy list. */
21         RandomListNode* new_head = new RandomListNode(0);
22         RandomListNode* new_run;
23         run = head;
24         new_head -> next = head -> next;
25         while (run) {
26             new_run = run -> next;
27             run -> next = new_run -> next;
28             if (run -> next)
29                 new_run -> next = new_run -> next -> next;
30             run = run -> next;
31         }
32         return new_head -> next;
33     }
34 };

 

目录
相关文章
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
44 1
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
50 0
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
37 0
|
6月前
|
算法 索引 Python
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
69 0
|
6月前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
51 0
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 19. 删除链表的倒数第N个节点 Remove Nth Node From End of List
LeetCode 19. 删除链表的倒数第N个节点 Remove Nth Node From End of List
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行