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结点,再连接随机结点,最后分离链表。
 
  1. /**
  2. * Definition for singly-linked list with a random pointer.
  3. * struct RandomListNode {
  4. * int label;
  5. * RandomListNode *next, *random;
  6. * RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. RandomListNode *copyRandomList(RandomListNode *head) {
  12. unordered_map<RandomListNode*,RandomListNode*> umap;
  13. RandomListNode* newHead = NULL;
  14. RandomListNode* cur = head;
  15. RandomListNode* node_pre = NULL;
  16. RandomListNode* node = NULL;
  17. while(cur){
  18. node = new RandomListNode(cur->label);
  19. umap[cur] = node;
  20. cur == head ? newHead = node :node_pre->next = node;
  21. node_pre = node;
  22. cur = cur->next;
  23. }
  24. cur = head;
  25. while(cur){
  26. umap[cur]->random = cur->random ? umap[cur->random] : NULL;
  27. cur = cur->next;
  28. }
  29. return newHead;
  30. }
  31. };
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. 使用oracle来计算方差及标准差

    /* Formatted on 5/24/2012 4:15:58 PM (QP5 v5.149.1003.31008) */ SELECT deptno,       ename,          ...

  2. C#避免过长的IF和Switch分支的方法

    C#避免过长的IF和Switch分支的方法 1.最蠢形态 //很丑有没有! //这个分支要是一两个还是可以接受的 class Program { static void Main(string[] a ...

  3. poj 3959 Alignment of Code <vector>“字符串”

    Description You are working in a team that writes Incredibly Customizable Programming Codewriter (IC ...

  4. 层层递进Struts1(六)自定义转换器

    Struts提供的类型转换有限,如果我们强行使用没有的类型转换,则会出现错误,以Date类型为例: org.apache.catalina.core.StandardWrapperValve invo ...

  5. java读取xml(当xml放在包里时)

  6. jQuery插件教程

    http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html   非常不错的jQuery插件教程

  7. gulp压缩js

    1.安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 2.查看nodejs的版本号 3.npm ...

  8. linux io优化

    场景:xml文件解析入库:并备份 问题:磁盘io异常,经常100%busy: linux io优化方法: 1.修改磁盘挂着参数,修改为writeback模式:对于文件读取频繁的可以设置noatime: ...

  9. http://www.cnblogs.com/Joyes1989/archive/2013/06/28/3161739.html centos 输入法安装切换

    昨天装了一个centos  安装输入法的时候  让我有点纠结  全英文的 读不懂

  10. FATE(费用背包,没懂)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...