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.

分析:

我们知道如果是简单的copy List 的话,那么我们只需要从头到尾遍历下来,new出对应个数的Node,并把它们的连接关系设置好就可以了,但是这道题目中每个节点Node出现了Random属性,也就意味着可能当前结点Node所依赖的那个Random对应的结点还没有被创建出来。

如何能够找到新链表每个节点 random 域 所指向的节点呢??

思路: Step 1: 首先指向在原链表的每个节点后面,复制一个新的节点,原链表长度变为 2 倍

   random 指针指向的是 原链表节点 random 指针指向的节点的后面的那个节点

   Step 2: 将链表拆成两个 lists.

下面的代码不是自己写的,自己写的那个不知道为啥,超时了,也不知道错在哪里,这题还有一种用Map的解法,没去看,到时候好好想想········

/**
* 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 *tHead = head;
RandomListNode *next = NULL;
while(tHead)
{
next = tHead->next;
RandomListNode *node = new RandomListNode(tHead->label);
node->next = tHead->next;
//node->random = tHead->random;
tHead->next = node;
tHead= next;
}
tHead = head;
while(tHead)
{
if(tHead->random) tHead->next->random = tHead->random->next;
tHead = tHead->next->next;
}
RandomListNode *retHead = NULL;
RandomListNode *tRet = NULL; tHead = head;
RandomListNode *next2 = NULL;
while(tHead)
{
if(retHead == NULL)
{
next2 = tHead->next->next;
retHead = tHead->next;
tRet = retHead;
tHead->next = next2;
tHead = next2;
}
else
{
next2 = tHead->next->next;
tRet->next = tHead->next;
tHead->next = next2;
tHead = next2;
tRet = tRet->next;
} }
return retHead;
}
};

  

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. LeetCode138:Copy List with Random Pointer

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

  8. 【Copy List with Random Pointer】cpp

    题目: 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. Linux应用编程之串口操作20170901

    主要介绍在Linux应用程序下对串口的操作: 1.串口初始化 int InitCom() { int Ret; Ret = SerailComm.OpenCom( ComPortDevPath, 0 ...

  2. php数组定义

    $arr = array( ,), //是’0' ,不是[‘0’] ,), ,), ,), ); $arr = [ '0' => array(2,3), //是’0' ,不是[‘0’] '1' ...

  3. python shell

    os.system(cmd) 命令执行结果 0或者1 output = os.popen(cmd) print output.read() 通过 os.popen() 返回的是 file read 的 ...

  4. tomcat maven插件启动报错tomcat-users.xml cannot be read

    tomcat maven插件启动报错tomcat-users.xml cannot be read [ERROR] Failed to execute goal org.codehaus.mojo:t ...

  5. [DeeplearningAI笔记]卷积神经网络2.9-2.10迁移学习与数据增强

    4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.9迁移学习 迁移学习的基础知识已经介绍过,本篇博文将介绍提高的部分. 提高迁移学习的速度 可以将迁移学习模型冻结的部分看 ...

  6. HTML常用标签-手打抄录-来自-烟雨飘零-拜谢

    HTML常用标签及其全称 <a href="#">a 超级链接(anchor)</a>    <abbr title="abbreviati ...

  7. 你知道吗?衡量 Web 性能的几个关键指标

    自网站诞生以来,响应速度/响应时间一直都是大家关心的话题,而速度慢乃是网站的一个杀手,正当大家以为四核和宽带能力的提升能够解决这些问题时,Wi-Fi和移动设备为热点移动互联网又悄然兴起. 在2006年 ...

  8. 【洛谷 P3338】 [ZJOI2014]力(FFT)

    题目链接 \[\Huge{E_i=\sum_{j=1}^{i-1}\frac{q_j}{(i-j)^2}-\sum_{j=i+1}^{n}\frac{q_j}{(i-j)^2}}\] 设\(A[i]= ...

  9. js获得页面鼠标位置

    1.客户区坐标位置:clientX,clientY 鼠标相对于在当前页面可视范围左上角的位置 2.页面坐标位置:pageX,pageY 鼠标相对于页面左上角的位置(受滑动等影响,例如pageY=cli ...

  10. Linux命令之uptime

    这是什么 uptime用来查看系统已经启动了多长时间了. 它显示的信息和w命令的头(第一行)是一样一样的. 举个栗子 举一个实际的应用场景: 比如发现服务器上的某些没有加入开机启动的服务挂了一片,这个 ...