[LeetCode] Copy List with Random Pointe
题目的关键是要让新链表和原有链表发送关联,可以通过这种关联来设置新链表的random pointer
思路:将新链表的元素插入到原有链表元素的后面,如下图所示,就可以根据原有链表的radom->next 就是新链表的random指针
所以分3步骤:
1 新元素插入
2 设置新链表的random
3 拆分大链表,回复old link 和new link
/**
* 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) { RandomListNode* pOld = head;
RandomListNode* pNew = NULL;
RandomListNode* pRes = NULL; if(head == NULL) return NULL; // insert every new node after old new node
while(pOld)
{
pNew = new RandomListNode(pOld->label);
if(pOld == head) pRes = pNew;
pNew->next = pOld->next;
pOld->next = pNew;
pOld = pNew->next;
} pOld = head;
// constrct new's random pointer
while(pOld)
{
pNew = pOld->next;
if(pOld->random == NULL)
pNew->random == NULL;
else
pNew->random = pOld->random->next;
pOld = pNew->next;
} // recover old's and new's next pointer
//恢复old list 和new list pOld = head; while(pOld)
{
pNew = pOld->next;
if(pNew == NULL)
pOld->next = NULL;
else
pOld->next = pNew->next;
pOld = pNew;
} return pRes;
}
};
[LeetCode] Copy List with Random Pointe的更多相关文章
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- 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 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- 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 &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
随机推荐
- 变态跳台阶-一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
class Solution { public: int jumpFloorII(int number) { ) ; ) ; *jumpFloorII(number-); } };
- Redis做消息队列文章两篇
介绍:http://www.cnblogs.com/lhfcws/p/3732535.html 具体做法:http://shift-alt-ctrl.iteye.com/blog/1867454 另外 ...
- ubuntu上怎么设置默认python命令是执行python3而不是python2
来源:https://segmentfault.com/q/1010000003713912 alternatives这么好的机制用起来呀. shell里执行: sudo update-alterna ...
- Caffe学习系列(1):安装配置ubuntu14.04+cuda7.5+caffe+cudnn
一.版本 linux系统:Ubuntu 14.04 (64位) 显卡:Nvidia K20c cuda: cuda_7.5.18_linux.run cudnn: cudnn-7.0-linux-x6 ...
- opencv3学习:reshape函数
在opencv中,reshape函数比较有意思,它既可以改变矩阵的通道数,又可以对矩阵元素进行序列化,非常有用的一个函数. 函数原型: C++: Mat Mat::reshape() const 参数 ...
- 20135335郝爽 & 20135304刘世鹏 实验一
北京电子科技学院(BESTI) 实 验 报 告 课程: 密码系统设计基础 ...
- 第一章 Javscript的数据类型
任何编程语言,都会讲到数据类型,那么我在这里也简述下Js的数据类型,在js里判断一个变量的数据类型用typeof() 简单数据类型undefined: 代表一切未知的事物,啥都没有,无法想象,代码 ...
- python3 入门 (三) 函数与lambda表达式、闭包
函数 是组织好的.可重复使用的.用来实现单一或相关联功能的代码段. 函数代码块以def关键词开头,后接函数标识符名称和圆括号() 任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数 函 ...
- Bootstrap3.0学习第十三轮(导航条)
详情请查看http://aehyok.com/Blog/Detail/20.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- AngularJS开发指南12:AngularJS的模板,CSS,数据绑定详解
模板 AngularJS模板是一种声明式的规则.它包含了模型和控制器的信息,最后会被渲染成用户在浏览器中看到的视图.它是静态的DOM,包含HTML,CSS和AngularJS指定的元素和属性.Angu ...