类同:剑指 Offer 题目汇总索引第26题

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.

说明:分三步, 1. 每个节点复制其本身并链接在后面。 2, 复制随机指针。 3, 拆分链表。

  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. if(head == NULL) return NULL;
  13. RandomListNode *head2, *p, *p2;
  14. p = head2 = head;
  15. while(p) {
  16. RandomListNode *s = new RandomListNode(p->label);
  17. s->next = p->next;
  18. p->next = s;
  19. p = s->next;
  20. }
  21. p = head;
  22. while(p) {
  23. if(p->random) p->next->random = p->random->next;
  24. p = p->next->next;
  25. }
  26. head2 = p2 = head->next; p = head;
  27. while(p) {
  28. p->next = p->next->next;
  29. p = p->next;
  30. if(p2->next) {
  31. p2->next = p2->next->next;
  32. p2 = p2->next;
  33. }
  34. }
  35. return head2;
  36. }
  37. };

16. Copy List with Random Pointer的更多相关文章

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

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

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

  3. Copy List with Random Pointer leetcode java

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

  4. LintCode - Copy List with Random Pointer

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

  5. [Leetcode Week17]Copy List with Random Pointer

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

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

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

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

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

  8. Leetcode Copy List with Random Pointer

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

  9. 【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 ...

随机推荐

  1. 1900. Brainwashing Device

    http://acm.timus.ru/problem.aspx?space=1&num=1900 题目大意: 有N个车站,相邻车站之间形成一个段,这样就有N-1个段,每个段最多可以放一个洗脑 ...

  2. 2015 "BestCoder Cup" Champion

    这场比赛我没有参加,不过就算参加了也估计是被完虐.于是看着题解把大部分题目都搞了一遍. T1:Movie(hdu 5214) 题目大意: 给出N个区间,问能否选出3个互不相交的区间. N<=10 ...

  3. SQL Server基本函数

    1. 字符串函数 1.1 datalength( char_expr ) ,返回表达式的字节数,不包含尾随空格 返回类型:如果 expression 的数据类型为 varchar(max).nvarc ...

  4. wine install 32bit netframewok

    WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg env WINEARCH=win32 WINEPREFIX=~/.wine32 winetricks dotne ...

  5. 在MAC OS X上如何启用crontab?

    project: blog target: how-to-enable-crontab-on-osx.md date: 2015-12-16 status: publish tags: - OS X ...

  6. BZOJ 4034 BIT & Dfs序

    调了恒久突然发现输出优化忘记带负号了.. 就是差分树状数组维护Dfs序即可. #include <iostream> #include <cstring> #include & ...

  7. 服务器自己用户名下编译gcc

    要点: 1.上传gcc 学习命令 scp 具体格式: scp local_file remote_username@remote_ip:remote_folder scp /home/linux/so ...

  8. MVC解决方案发布IIS 登录页面需要输入两次帐号问题

    IIS项目在本地VS2013 解决方案中正常登录可以进入.发布IIS时出现需要输入两次帐号密码进入主页面最终发现是web.config文件配置问题 web.config 默认配置 <authen ...

  9. block捕获自动变量和对象

    一.捕获自动变量值 首先看一个经典block面试题: int val = 10; void (^blk)(void) = ^{printf("val=%d\n",val);}; v ...

  10. Python 基礎 - 字符編碼

    Python 解釋器在加載 .py 文件中的代碼時,會對內容進行編碼 (默認 ascill) ASCII (American Standard Code for Information Interch ...