16. Copy List with Random Pointer
类同:剑指 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, 拆分链表。
- /**
- * 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) {
- if(head == NULL) return NULL;
- RandomListNode *head2, *p, *p2;
- p = head2 = head;
- while(p) {
- RandomListNode *s = new RandomListNode(p->label);
- s->next = p->next;
- p->next = s;
- p = s->next;
- }
- p = head;
- while(p) {
- if(p->random) p->next->random = p->random->next;
- p = p->next->next;
- }
- head2 = p2 = head->next; p = head;
- while(p) {
- p->next = p->next->next;
- p = p->next;
- if(p2->next) {
- p2->next = p2->next->next;
- p2 = p2->next;
- }
- }
- return head2;
- }
- };
16. Copy List with Random Pointer的更多相关文章
- 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 ...
- 【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 ...
随机推荐
- 1900. Brainwashing Device
http://acm.timus.ru/problem.aspx?space=1&num=1900 题目大意: 有N个车站,相邻车站之间形成一个段,这样就有N-1个段,每个段最多可以放一个洗脑 ...
- 2015 "BestCoder Cup" Champion
这场比赛我没有参加,不过就算参加了也估计是被完虐.于是看着题解把大部分题目都搞了一遍. T1:Movie(hdu 5214) 题目大意: 给出N个区间,问能否选出3个互不相交的区间. N<=10 ...
- SQL Server基本函数
1. 字符串函数 1.1 datalength( char_expr ) ,返回表达式的字节数,不包含尾随空格 返回类型:如果 expression 的数据类型为 varchar(max).nvarc ...
- wine install 32bit netframewok
WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg env WINEARCH=win32 WINEPREFIX=~/.wine32 winetricks dotne ...
- 在MAC OS X上如何启用crontab?
project: blog target: how-to-enable-crontab-on-osx.md date: 2015-12-16 status: publish tags: - OS X ...
- BZOJ 4034 BIT & Dfs序
调了恒久突然发现输出优化忘记带负号了.. 就是差分树状数组维护Dfs序即可. #include <iostream> #include <cstring> #include & ...
- 服务器自己用户名下编译gcc
要点: 1.上传gcc 学习命令 scp 具体格式: scp local_file remote_username@remote_ip:remote_folder scp /home/linux/so ...
- MVC解决方案发布IIS 登录页面需要输入两次帐号问题
IIS项目在本地VS2013 解决方案中正常登录可以进入.发布IIS时出现需要输入两次帐号密码进入主页面最终发现是web.config文件配置问题 web.config 默认配置 <authen ...
- block捕获自动变量和对象
一.捕获自动变量值 首先看一个经典block面试题: int val = 10; void (^blk)(void) = ^{printf("val=%d\n",val);}; v ...
- Python 基礎 - 字符編碼
Python 解釋器在加載 .py 文件中的代碼時,會對內容進行編碼 (默認 ascill) ASCII (American Standard Code for Information Interch ...