Copy List with Random Pointer——技巧
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.
分析:
我们知道如果是简单的copy List 的话,那么我们只需要从头到尾遍历下来,new出对应个数的Node,并把它们的连接关系设置好就可以了,但是这道题目中每个节点Node出现了Random属性,也就意味着可能当前结点Node所依赖的那个Random对应的结点还没有被创建出来。
如何能够找到新链表每个节点 random 域 所指向的节点呢??
思路: Step 1: 首先指向在原链表的每个节点后面,复制一个新的节点,原链表长度变为 2 倍
random 指针指向的是 原链表节点 random 指针指向的节点的后面的那个节点
Step 2: 将链表拆成两个 lists.
下面的代码不是自己写的,自己写的那个不知道为啥,超时了,也不知道错在哪里,这题还有一种用Map的解法,没去看,到时候好好想想········
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *tHead = head;
RandomListNode *next = NULL;
while(tHead)
{
next = tHead->next;
RandomListNode *node = new RandomListNode(tHead->label);
node->next = tHead->next;
//node->random = tHead->random;
tHead->next = node;
tHead= next;
}
tHead = head;
while(tHead)
{
if(tHead->random) tHead->next->random = tHead->random->next;
tHead = tHead->next->next;
}
RandomListNode *retHead = NULL;
RandomListNode *tRet = NULL; tHead = head;
RandomListNode *next2 = NULL;
while(tHead)
{
if(retHead == NULL)
{
next2 = tHead->next->next;
retHead = tHead->next;
tRet = retHead;
tHead->next = next2;
tHead = next2;
}
else
{
next2 = tHead->next->next;
tRet->next = tHead->next;
tHead->next = next2;
tHead = next2;
tRet = tRet->next;
} }
return retHead;
}
};
Copy List with Random Pointer——技巧的更多相关文章
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- LeetCode138:Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 【Copy List with Random Pointer】cpp
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- CSS3制作3D水晶糖果按钮
本人仿照20个漂亮 CSS3 按钮效果及优秀的制作教程中的BonBon(Candy)Button实现了其棒棒糖果按钮,如下图所示: 在线演示地址见here. 使用完全使用CSS实现,无需JS.源码如下 ...
- 洛谷10月月赛R2·浴谷八连测R3题解
早上打一半就回家了... T1傻逼题不说了...而且我的写法比题解要傻逼很多T T T2可以发现,我们强制最大值所在的块是以左上为边界的倒三角,然后旋转4次就可以遍历所有的情况.所以二分极差,把最大值 ...
- Codeforces 480.E Parking Lot
E. Parking Lot time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 894.E Ralph and Mushrooms
E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input sta ...
- P1582 倒水 (数学)
P1582 倒水 题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把 ...
- 2018-2019, ICPC, Asia Yokohama Regional Contest 2018 K
传送门:https://codeforces.com/gym/102082/attachments 题解: 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ...
- c# extern 关键字
TEST.DLL 项目引用TEST.DLL 调用其中的方法 结果如下:
- 驱动学习5: zynq实现点亮led
驱动代码: #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #i ...
- springboot+spring session+redis+nginx实现session共享和负载均衡
环境 centos7. jdk1.8.nginx.redis.springboot 1.5.8.RELEASE session共享 添加spring session和redis依赖 <depen ...
- 782C. Andryusha and Colored Balloons DFS
Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...