图解:

此题过程分为三个阶段,分别是 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. 【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1600 说好的今天开始刷水.. 本题一开始我以为是排列组合,但是自己弱想不出来,只想到了如果四边有一条 ...

  2. esper 事件引擎,各种事件类型示例代码

    原创文章 转载请注明出处 package com.hp.iot.engine.esper; import java.util.ArrayList; import java.util.HashMap; ...

  3. Strong TLS configuration on servers

    - Use certificates with at least sha-256 hash algorithms (including intermediate certificates).- Use ...

  4. Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks

    https://code.google.com/codejam/contest/635101/dashboard#s=p1   Problem A flock of chickens are runn ...

  5. spring+struts2+mybatis

    struts2.2.3 + spring3.1.0 + mybatis3.1.0集成简单demo 项目下载地址:http://download.csdn.net/detail/afgasdg/4171 ...

  6. 【回文串-Manacher】

    Manacher算法能够在O(N)的时间复杂度内得到一个字符串以任意位置为中心的回文子串.其算法的基本原理就是利用已知回文串的左半部分来推导右半部分. 转:http://blog.sina.com.c ...

  7. jQuery.Validate验证库详解

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  8. MatLab GUI Use Command for Debug 界面调试的一些方法

    在MatLab的GUI界面编程,我们在调试的时候需要打印出一些变量,那么介绍下我用到的两种调试方法: 第一种,使用弹出对话框来打印变量,要注意的是打印的东西必须是string类型的,所以其他类型的变量 ...

  9. android开发事件监听

    第一种:匿名内部类作为事件监听器类 大部分时候,事件处理器都没有什么利用价值(可利用代码通常都被抽象成了业务逻辑方法),因此大部分事件监听器只是临时使用一次,所以使用匿名内部类形式的事件监听器更合适, ...

  10. mac mysql环境配置

    安装mysql:http://www.mysql.com/downloads/ 找到 MySQL Community Edition (GPL) Community (GPL) Downloads » ...