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 ...
随机推荐
- BZOJ1224: [HNOI2002]彩票
Description 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票,不同的彩民的彩票上的选择不同.每次抽奖将抽出两个自然数X和 ...
- iOS - AVAudioPlayer 音频播放
前言 NS_CLASS_AVAILABLE(10_7, 2_2) @interface AVAudioPlayer : NSObject @available(iOS 2.2, *) public c ...
- CodeBlocks的汉化、主题美化及其调试功能的实现
破事水 最近由于Cfree5经常崩溃+调试语句运行速度比较慢,想尝试一下另一个听说很好用的IDE Code::Blocks. 先上官网的安装包(自带mingw,安装完可以直接用,适用于windows系 ...
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- li标签行内元素高度及居中
<head> <title><title> <style type="text/css"> * { padding: 0px; ma ...
- svchost.exe是什么?为什么一直在运行
原文:http://www.howtogeek.com/howto/windows-vista/what-is-svchostexe-and-why-is-it-running/ 自己简单翻译了下,图 ...
- 《Java核心技术卷一》笔记 多线程
有时,我们需要在一个程序中同时并行的处理多个任务,如播放器一边要播放音乐同时还要不断更新画面显示,或者是一边执行耗时任务,UI还能一边继续响应各种事件.还有的时候,一个任务需要很长时间才能完成,如果分 ...
- maven Ubuntu14.04 安装
参考:linux上安装使用maven 下载链接:官网Download 解压. 在root用户下执行: cd /opt mkdir maven chmod 755 /opt/maven tar -zvx ...
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- Bootstrap页面布局17 - BS选项卡
代码结构: <div class='container-fluid'> <h2 class='page-header'>Bootstrap 选项卡</h2> < ...