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、复制后面一个节点的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.的更多相关文章
- [Linked List]Copy List with Random Pointer
Total Accepted: 53943 Total Submissions: 209664 Difficulty: Hard A linked list is given such that ea ...
- [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 ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- 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 bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- 【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 ...
- Copy List with Random Pointer [LeetCode]
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- 编写一个名为Test的主类,类中只有一个主方法; 在主方法中定义一个大小为50的一维整型数组,数组名为x,数组中存放着{1, 3,5,…,99}输出这个数组中的所有元素,每输出十个换一行;在主方法中定义一 个大小为10*10的二维字符型数组,数组名为y,正反对角线上存的是‘*’,其余 位置存的是‘#’;输出这个数组中的所有元素。
package liu0915; import java.util.Random; public class Test0915sz { public static void main(String[] ...
- 一、saltstack简介和安装
系统环境:CentOS6.5 准备yum源: epel源(包含了saltstack的包).阿里源(CentOS-Base.repo) Host解析文件: # cat /etc/hosts 192.16 ...
- 数组机、局域网ip查找
cmd ipconfig 以太网适配器 VMware Network Adapter VMnet8: IPv4 地址 . . . . . . . . . . . . : 192.168.233.1
- Educational Codeforces Round 15 C. Cellular Network(二分)
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Web中的图标
随着时代的变迁与技术的不断的更新,在当今这个时代,Web中的图标(Icons)不再仅仅是局限于<img>.除了<img>直接调用Icons文件之外,还有Sprites(俗称雪碧 ...
- JavaScript系列:常用方法
文本框输入实时验证身份证号 charAt(索引)<=>indexOf(字符) <!DOCTYPE html> <head> <meta charset=&qu ...
- POI格式化Cell样式
通过一个实例演示怎样通过POI设置Excel单元格的边框.字体.颜色.大小.下划线.合并.对齐方式. Excel文件如下: Java代码 package my.excel; impor ...
- 解决Eclipse Debug 的source not found问题
最近在做Android 4.4系统的定制开发(RockIII)进行Debug时,并打上断点,运行到断点处时,Debug窗口出现source not found问题(没有自动关联程序编码): 解决办法: ...
- Gitolite配置管理和GIT基本操作
简述公司版gitolite的项目配置与管理 1. 基于秘钥对的管理 1.1 客户端(需要访问代码库的机器)生成秘钥对,采用RSA加密ssh-keygen -t rsa -f path_to_store ...
- [ZZ] 基于DirectX shader的Per-pixel lighting实现
这个特效需要用到DX11 UAV吗? http://blog.tianya.cn/blogger/post_show.asp?BlogID=510979&PostID=5665974 Intr ...