Total Accepted: 53943 Total Submissions: 209664 Difficulty: Hard

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.

 
o(n)空间复杂度,代码比价简单。网上还流传一种o(1)空间复杂度的解法,大致的过程就是先拷贝next结点,再连接随机结点,最后分离链表。
 
/**
* 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;
}
};
Next challenges: (M) Clone Graph

[Linked List]Copy List with Random Pointer的更多相关文章

  1. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  2. 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 ...

  3. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  4. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  5. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  6. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  7. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  9. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

随机推荐

  1. html布局

    1.div <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...

  2. UGUI学习之InputField

    密码框在Context type 设置为Passwold 设置背景和调整字体颜色与透明度. 还有一个就是Toggle,(开关)要指定他的Graphic.

  3. Intel Core i7的整体操作

    Intel Core i7的整体操作(我们也称呼为Nehalem,他的项目代码名) 主要分成2个部分-指令控制单元Instruction Control Unit(ICU),负责从存储器读出指令序列, ...

  4. 身份证校验程序(上)- 零基础入门学习Delphi48

    身份证校验程序 让编程改变世界 Change the world by program [caption id="attachment_2699" align="alig ...

  5. Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 ...

  6. innobackupex 使用说明

    1.创建备份相关用户 '; grant reload,lock tables,replication client,process,super on *.* to 'backuper'@'127.0. ...

  7. 运行时数据区即内存分配管理——JVM之六

    内存分配结构,请参考: http://iamzhongyong.iteye.com/blog/1333100

  8. rpm包制作

    ubuntu下先下载sudo apt-get install rpm就行了. 然后测试下rpm和rpmbuild命令都是存在的.好了,OK. rpm安装包的制作有严格的自定义的路径,这个路径是在/us ...

  9. 【转】Android C程序也可自己手动用交叉编译器编译 (

    原文网址:http://blog.sina.com.cn/s/blog_533074eb0101ez5q.html Android 编译环境 本身比较复杂,且不像普通的编译环境:只有顶层目录下才有 M ...

  10. UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>

    C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...