[Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/copy-list-with-random-pointer/description/
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.
Solution
/*
struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (head == NULL)
return NULL;
RandomListNode *node1, *node2;
for (node1 = head; node1 != NULL; node1 = node1 -> next -> next) {
node2 = new RandomListNode(node1 -> label);
node2 -> next = node1 -> next;
node1 -> next = node2;
}
RandomListNode* resNode = head -> next;
for (node1 = head; node1 != NULL; node1 = node1 -> next -> next) {
node2 = node1 -> next;
node2 -> random = node1 -> random ? node1 -> random -> next : NULL;
}
for (node1 = head; node1 != NULL; node1 = node1 -> next) {
node2 = node1 -> next;
node1 -> next = node2 -> next;
node2 -> next = node2 -> next ? node2 -> next -> next : NULL;
}
return resNode;
}
};
解题描述
这道题目是关于链表深拷贝的变种。最难的一点就是不同于传统的链表,这道题中的链表每个节点会带有一个随机节点指针,指向链表中的任意一个节点。所以拷贝的时候,不仅要完成链表的顺序拷贝,还要完成在新的链表中随机节点指针的拷贝。
解题思路上,关键是如何保证新旧链表中的随机节点指针指向的节点的位置在新旧链表中是一样的。如果我们能够在进行随机节点的指向的复制的时候,知道新旧链表中当前节点还有当前节点指向的随机节点之间的一一对应关系,就可以完成随机指向关系的复制。

如图所示,如果要复制1号节点到3号节点的随机指向关系,需要我们知道新旧链表中,1号节点和3号节点的对应关系。
所以在进行相对简单的顺序拷贝之前,我们可以先考虑保存新旧链表中的节点一一对应的关系。这里可以采用一个做法:在顺序拷贝每一个节点之后,将节点插入到原来的链表中,相当于一个新旧链表的merge操作,在后期再进行原链表的恢复:

如图所示,这样我们在拷贝1号节点的随机指向时,就可以通过原链表1号节点指向的random节点的next节点找到新链表1号节点应该指向的对应节点。而在原链表中进行游标顺序移动的时候,只需要每一步多走一次next。后面节点的随机指向关系拷贝以此类推。
而链表的恢复也相对简单,不做赘述。
[Leetcode Week17]Copy List with Random Pointer的更多相关文章
- Java for LeetCode 138 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【leetcode】Copy List with Random Pointer (hard)
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 138. Copy List with Random Pointer ----- java
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode _ Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 【 Copy List with Random Pointer 】 python 实现
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
随机推荐
- set类型没有单独取值功能 通过循环取值
set类型没有单独取值功能 通过循环取值
- 【bzoj4417】[Shoi2013]超级跳马 矩阵乘法
题目描述 现有一个n行m列的棋盘,一只马欲从棋盘的左上角跳到右下角.每一步它向右跳奇数列,且跳到本行或相邻行.跳越期间,马不能离开棋盘.例如,当n = 3, m = 10时,下图是一种可行的跳法. ...
- 3. 无重复字符的最长子串(O(N))
给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. 给定 ...
- [洛谷P5057][CQOI2006]简单题
题目大意:有一个长度为$n$的$01$串,两个操作: $1\;l\;r:$把区间$[l,r]$翻转($0->1,1->0$) $2\;p:$求第$p$位是什么 题解:维护前缀异或和,树状数 ...
- [HNOI2012][BZOJ2732] 射箭 [二分+半平面交]
题面 BZOJ题面 思路 半平面交代码讲解戳这里,用的就是这道题 我们射箭的函数形如$y=Ax^2+Bx$ 考虑每一个靶子$(x_0,y_1,y_2)$,实际上是关于$A,B$的不等式限制条件 我们只 ...
- BZOJ3140:[HNOI2013]消毒——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3140 https://www.luogu.org/problemnew/show/P3231 最近在 ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- HDOJ.1029 Ignatius and the Princess IV(map)
Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...
- 学习web安全之--初识安全
随笔:随着互联网行业的飞速发展,互联网行业可谓日新月异,然而在繁华的背后,大多的互联网公司对于网络安全还是处于无重视,不作为的阶段,而作为一个程序员,如果也对信息安全视而不见的话,那将是这个公司的噩梦 ...
- postgresql pgagent 的安装及使用
pgagent 作为postgresql的一个任务调度代理,在postgresql 9.0 以前 是附带在pgadmin 包下面的,只是默认不安装,9.0之后作为了一个单独是的安装包.所以要使用pga ...