[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 ...
随机推荐
- php基础24:数组range
<?php $numbers = range(1, 10); echo "<pre>"; var_dump($numbers); echo "</ ...
- Sql语句里的递归查询
Sql语句里的递归查询 SqlServer2005和Oracle 两个版本 以前使用Oracle,觉得它的递归查询很好用,就研究了一下SqlServer,发现它也支持在Sql里递归查询举例说明:Sql ...
- pandas 练习
from pandas import Series, DataFrame # Series接收list或dict作为一维数据 #两个属性:values, index #① s1 = Series([4 ...
- 基于.NET平台常用的框架整理 (转)
http://www.cnblogs.com/hgmyz/p/5313983.html 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生了 ...
- 免费Flash图表工具FusionChart
图表显示是很多开发工作所必不可少的一项功能,今天我介绍一个前段时间发现的免费的Flash图表开发工具,可以通过Adobe Flash实现数据的图表化,动态化以及相互交互. FusionChart是一个 ...
- 20135220谈愈敏Linux Book_3
第3章 进程管理 进程是Unix操作系统抽象概念中最基本的一种,进程管理是操作系统的心脏所在. 3.1 进程 进程:处于执行期的程序以及相关的资源的总称. 线程:在进程中活动的对象,拥有独立的程序计数 ...
- 实验一(不知道怎么上传.docx格式)
北京电子科技学院(BESTI) 实 验 报 告 课程:深入理解计算机系统 班级:1353 姓名:魏静静 文艺 刘虹辰 学号:20135302 20135331 20 ...
- [软件测试]Linux环境中简单清爽的Google Test (GTest)测试环境搭建(初级使用)
本文将介绍单元测试工具google test(GTEST)在linux操作系统中测试环境的搭建方法.本文属于google test使用的基础教程.在linux中使用google test之前,需要对如 ...
- 程序员的出路在哪里?挣钱的机会来了续-福利来了,仿QQ界面,放出全部源码,打造创业框架及实现思路
上一篇:程序员的出路在哪里?挣钱的机会来了!, 原来搞技术,挣钱,不一定非得要多高精尖,有时候抓住小白用户,解决他们一个很小但是很常用的功能,也是一条很好的出路. 其实很多软件产品,要实现出来没有你想 ...
- Matrix67大牛推荐的省选知识点
时间复杂度(渐近时间复杂度的严格定义,NP问题,时间复杂度的分析方法,主定理)排序算法(平方排序算法的应用,Shell排序,快速排序,归并排序,时间复杂度下界,三种线性时间排序,外部排序)数论(整除, ...