图解:

此题过程分为三个阶段,分别是 1、负责后面一个节点,并且将这个节点插入到原来链表中  2、复制后面一个节点的random指针。 3 拆分组合链表为两部分。

第一部分代码:

  while(currentnode!=null){//复制节点,并且将节点插入到该节点的后面
RandomListNode clonenode=new RandomListNode(currentnode.label);
clonenode.next=currentnode.next;
currentnode.next=clonenode;
currentnode=clonenode.next;
}

第二部分 代码:

  currentnode=head;
while(currentnode!=null){//复制随机指针。
RandomListNode clonenode=currentnode.next;
if(currentnode.random!=null){
clonenode.random=currentnode.random.next;//这里要指向随机指针的后一个,因为是复制滴啊,千万不要忘记了。
}
currentnode=clonenode.next;
}

第三部分代码:

  RandomListNode cloneHead=head.next;
currentnode=head;
while(currentnode.next!=null){
RandomListNode temp=currentnode.next;
currentnode.next=temp.next;
currentnode=temp;
}

总的代码:

class RandomListNode {
int label;
RandomListNode next, random;
RandomListNode(int x) { this.label = x; }
};
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head==null)return null;
RandomListNode currentnode=head;
while(currentnode!=null){//复制节点,并且将节点插入到该节点的后面
RandomListNode clonenode=new RandomListNode(currentnode.label);
clonenode.next=currentnode.next;
currentnode.next=clonenode;
currentnode=clonenode.next;
}
currentnode=head;
while(currentnode!=null){//复制随机指针。
RandomListNode clonenode=currentnode.next;
if(currentnode.random!=null){
clonenode.random=currentnode.random.next;//这里要指向随机指针的后一个,因为是复制滴啊,千万不要忘记了。
}
currentnode=clonenode.next;
} RandomListNode cloneHead=head.next;
currentnode=head;
while(currentnode.next!=null){
RandomListNode temp=currentnode.next;
currentnode.next=temp.next;
currentnode=temp;
}
return cloneHead; }
}

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. [Linked List]Copy List with Random Pointer

    Total Accepted: 53943 Total Submissions: 209664 Difficulty: Hard A linked list is given such that ea ...

  2. [Algo] 131. Deep Copy Linked List With Random Pointer

    Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...

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

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

  4. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

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

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

  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 bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

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

  9. Copy List with Random Pointer [LeetCode]

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

随机推荐

  1. 【POJ】2954 Triangle(pick定理)

    http://poj.org/problem?id=2954 表示我交了20+次... 为什么呢?因为多组数据我是这样判断的:da=sum{a[i].x+a[i].y},然后!da就表示没有数据了QA ...

  2. eclipse远程调试Tomcat方法

    1.Linux中配置tomcat在catalina.sh中添加如下CATALINA_OPTS="-Xdebug  -Xrunjdwp:transport=dt_socket,address= ...

  3. python的几个常用内置函数

    dir()查看属性(函数和数据对象) help()查看具体的帮助文档 id() 用来查看数据对象的地址 split 分隔(str ---> list): >>> s=" ...

  4. codeacademy

    codeacademy http://link.zhihu.com/?target=https%3A//www.youtube.com/playlist%3Flist%3DPLP6DbQBkn9ymG ...

  5. Shrink磁盘

      30857(分区的总容量) =  10160(可修改的这个10610表示Shrink之后的空闲分区) + 20697(Shrink之后分区中占用掉的容量)

  6. 获取文本文件的第N行内容

    在PowerShell中,可以通过Get-Content这个cmdlet来获取文本文件的内容.Get-Content将一个文本文件读取到一个数组中,每一个数组元素就是文件的一行内容.比如一个文本文件内 ...

  7. MONGODB 计算机服务

    安装完成后,在 BIN目录执行如下 E:\APMServ5.2.6\MongoDb\bin>mongod --logpath E:\APMServ5.2.6\MongoDb\logs\Mongo ...

  8. CSS权威指南 - 浮动和定位 1

    定位 定位的想法很简单元素框相对于正常位置出现在哪里. 定位:static,相对, 绝对, fixed, 继承 static就是默认的位置 相对就是相对于默认位置的偏移.原来的static定位位置依然 ...

  9. 用MSoffice里的绘图工具

    试过一些绘图表的工具,在xbeta推荐的替代visio一文中介绍的一些软件.之前用得最多的就是Dia,在linux下也有.现在才发现在微软的office下的绘图工具已经足够我使用了,不需要专业的图形符 ...

  10. Web 在线文件管理器学习笔记与总结(3)创建文件

    ① 创建文件 a. 文件名的合法性:不能包含 \/:*"<>| 等特殊字符 b. 检测当前目录下是否存在同名文件,如果存在提示请重命名后创建,如果不存在则直接创建 index.p ...