[Linked List]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.
/**
* 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) {
unordered_map<RandomListNode*,RandomListNode*> umap;
RandomListNode* newHead = NULL;
RandomListNode* cur = head;
RandomListNode* node_pre = NULL;
RandomListNode* node = NULL;
while(cur){
node = new RandomListNode(cur->label);
umap[cur] = node;
cur == head ? newHead = node :node_pre->next = node;
node_pre = node;
cur = cur->next;
}
cur = head;
while(cur){
umap[cur]->random = cur->random ? umap[cur->random] : NULL;
cur = cur->next;
}
return newHead;
}
};
[Linked List]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- ...
- [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(带random引用的单链表深拷贝)
问题: 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 ...
随机推荐
- html布局
1.div <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...
- UGUI学习之InputField
密码框在Context type 设置为Passwold 设置背景和调整字体颜色与透明度. 还有一个就是Toggle,(开关)要指定他的Graphic.
- Intel Core i7的整体操作
Intel Core i7的整体操作(我们也称呼为Nehalem,他的项目代码名) 主要分成2个部分-指令控制单元Instruction Control Unit(ICU),负责从存储器读出指令序列, ...
- 身份证校验程序(上)- 零基础入门学习Delphi48
身份证校验程序 让编程改变世界 Change the world by program [caption id="attachment_2699" align="alig ...
- Cleaning Shifts(POJ 2376 贪心)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15143 Accepted: 3875 ...
- innobackupex 使用说明
1.创建备份相关用户 '; grant reload,lock tables,replication client,process,super on *.* to 'backuper'@'127.0. ...
- 运行时数据区即内存分配管理——JVM之六
内存分配结构,请参考: http://iamzhongyong.iteye.com/blog/1333100
- rpm包制作
ubuntu下先下载sudo apt-get install rpm就行了. 然后测试下rpm和rpmbuild命令都是存在的.好了,OK. rpm安装包的制作有严格的自定义的路径,这个路径是在/us ...
- 【转】Android C程序也可自己手动用交叉编译器编译 (
原文网址:http://blog.sina.com.cn/s/blog_533074eb0101ez5q.html Android 编译环境 本身比较复杂,且不像普通的编译环境:只有顶层目录下才有 M ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...