图解:

此题过程分为三个阶段,分别是 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. 开学后,板刷usaco!

    我要开权限我要开权限我要开权限我要开权限我要开权限我要开权限我要开权限我要开权限 他们刷rank的太可恶了 没有usaco银组金组分类的oj太可恶了 没有usaco翻译的太可恶了 没有usaco数据的 ...

  2. tyvj100题留念

    全是水题萌萌哒~0~... 留个纪念

  3. 简单了解Flux,注意这是一个设计思想,是一个架构!!!!!

    在RN开发中,我们总是需要去更改一个组件个数据(也就是所谓的状态),我们一般是采用是在初始化的函数constror()(好像拼错了) 在这个函数里面申明我们的初始化数据(状态)eg:this.stat ...

  4. 提升 web 应用程序的性能(二)

    最佳实践 本章将略述能帮助您提升 web 应用程序性能的最佳实践. 减少 HTTP 请求数 每个 HTTP 请求都有开销,包括查找 DNS.创建连接及等待响应,因此削减不必要的请求数可减少不必要的开销 ...

  5. 【C语言】13-指针和字符串

    字符串回顾 一个字符串由一个或多个字符组成,因此我们可以用字符数组来存放字符串,不过在数组的尾部要加上一个空字符'\0'. char s[] = "李洪强"; 上面的代码定义了一个 ...

  6. android之发送短信程序

    首先改写activity_main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...

  7. HTML----锚点

    练习做一个邮箱的注册页面 1.12行2列的表格 2.表格里面嵌入表单 3.最后建一个锚点 4.写具体内容 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  8. 细说LastLogonTimeStamp

    微软在Windows Server 2003中引入了LastLogonTimeStamp属性.管理员们可以利用这个属性查看用户或者计算机最近是否登录过域.根据这些信息,管理员可以对长时间没有登录的账户 ...

  9. 8. Add the dashboard

    Controller Node: 1. sudo apt-get install apache2 memcached libapache2-mod-wsgi openstack-dashboard   ...

  10. CentOS 6.5下源码安装MySQL 5.6

    变量lower_case_file_system说明是否数据目录所在的文件系统对文件名的大小写敏感.ON说明对文件名的大小写不敏感,OFF表示敏感. 在my.cnf中[mysqld]更改lower_c ...