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.

原题链接:https://oj.leetcode.com/problems/copy-list-with-random-pointer/

题目:给定一个链表,当中的每一个节点包括有一个额外的随机指针指向链表中的随意其它节点或空。

返回链表的一份深度复制。

思路:复制源链表中的每个节点到新链表中(仅仅考虑next)。复制随机指针关系到新链表中(仅仅考虑random.next),此时新链表的长度是源链表的2倍了。此时修正随机指针为正确的指向关系。

	public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)
return null;
RandomListNode p = head;
while(p != null){
RandomListNode copy = new RandomListNode(p.label);
copy.next = p.next;
p.next = copy;
p = copy.next;
}
p = head;
while(p != null){
if(p.random != null)
p.next.random = p.random.next;
p = p.next.next;
}
p = head;
RandomListNode newHead = head.next;
while(p != null){
RandomListNode tmp = p.next;
p.next = tmp.next;
if(tmp.next != null)
tmp.next = tmp.next.next;
p = p.next;
}
return newHead;
} // Definition for singly-linked list with a random pointer.
class RandomListNode {
int label;
RandomListNode next, random; RandomListNode(int x) {
this.label = x;
}
}

LeetCode——Copy List with Random Pointer的更多相关文章

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

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

  2. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  3. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

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

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

  5. Leetcode Copy List with Random Pointer

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

  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 – Copy List with Random Pointer

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

  8. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  9. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

随机推荐

  1. socket编程之二:两种链接类型tcp和udp

    前面一篇文章说到了一些计算机网络的基础知识.引入了socket.从这节開始,就进入正题了. 一 概述 TCP:Transimission Control Protocol传输控制协议. UPD:Use ...

  2. R语言基础-数组和列表

    数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, dim),当中data必须 ...

  3. Qt容器类的对象模型及应用(线性结构篇:对于QList来说,sharable默认是false的,但对于接下来讲的QVector来说,sharable默认是true)

    用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...

  4. 使用roslyn编译website项目

    在Nuget中,添加Microsoft.CodeDom.Providers.DotNetCompilerPlatform. 在添加这个dll的时候,会自动在web.config中添加以下内容 < ...

  5. Control-of-Flow

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/control-of-flow The Transact-SQL contro ...

  6. mysqli一些常用方法及详解

    mysqli一些常用方法及详解 1.die()函数:表示向用户输出引号中的内容后,程序终止运行,提示定制的出错信息 ex: $conn = mysqli_connect("localhost ...

  7. windows安装gnvm安装教程,node多版本解决方案

    本文是实现windows下node多版本管理 Win10专业版 一.安装前准备 安装前请卸载node相关的所有东西!!! 二.gnvm下载 gnvm搜索 http://ksria.com/gnvm/ ...

  8. windows下安装reidis

    下载windows下redis安装包 https://github.com/MSOpenTech/redis/releases 这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了. 切换 ...

  9. UWP tips (与wp8.1的不同)

    一.异步调用之后,要更新UI时,代码如下 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&g ...

  10. C# Distanct List集合

    简单一维集合的使用 List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 }; List<str ...