Each of the nodes in the linked list has another pointer pointing to a random node in the list or null. Make a deep copy of the original list.

/**
* class RandomListNode {
* public int value;
* public RandomListNode next;
* public RandomListNode random;
* public RandomListNode(int value) {
* this.value = value;
* }
* }
*/
public class Solution {
public RandomListNode copy(RandomListNode head) {
// Write your solution here.
if (head == null) {
return head;
}
Map<RandomListNode, RandomListNode> map = new HashMap<>();
map.put(head, new RandomListNode(head.value));
RandomListNode dummy = new RandomListNode(0);
RandomListNode cur = dummy; while (head != null) {
if (!map.containsKey(head)) {
map.put(head, new RandomListNode(head.value));
}
// connect for the next of cur
cur.next = map.get(head);
if (head.random != null) {
if (!map.containsKey(head.random)) {
map.put(head.random, new RandomListNode(head.random.value));
}
cur.next.random = map.get(head.random);
}
head = head.next;
cur = cur.next;
}
return dummy.next;
}
}

[Algo] 131. Deep Copy Linked List With Random Pointer的更多相关文章

  1. [Algo] 132. Deep Copy Undirected Graph

    Make a deep copy of an undirected graph, there could be cycles in the original graph. Assumptions Th ...

  2. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

  3. [Linked List]Copy List with Random Pointer

    Total Accepted: 53943 Total Submissions: 209664 Difficulty: Hard A linked list is given such that ea ...

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

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

  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】Copy List with Random Pointer (hard)

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

  8. Copy List with Random Pointer [LeetCode]

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

  9. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

随机推荐

  1. 142-PHP trait的定义和使用

    <?php trait info{ //定义trait static function getinfo(){ return '这是一个'.__CLASS__.'类.<br />'; ...

  2. 在spark udf中读取hdfs上的文件

    某些场景下,我们在写UDF实现业务逻辑时候,可能需要去读取某个文件. 我们可以将此文件上传个hdfs某个路径下,然后通过hdfs api读取该文件,但是需要注意: UDF中读取文件部分最好放在静态代码 ...

  3. C++使用 scanf函数

    最近看了下C++,有些地方和c语言 还是不太一样的,当我在做输入一个数字的时候发现了错误.. 所以在使用scanf()的时候需要加下面这段在头文件  顶部: #define _CRT_SECURE_N ...

  4. 无法启动APK安装也,报异常FileUriExposedException

    无法打开APK安装页,报异常FileUriExposedException, https://juejin.im/entry/58e4643db123db15eb79a902

  5. 实验吧-杂项-MD5之守株待兔(时间戳&python时间戳函数time.time())

    其实也有点蒙圈,因为从没做过和时间戳有关的题. 打开网站,将系统密钥解密得到一串值,而自己的密钥解密是空的,既然说是要和系统匹配,就把解密得到的值以get方式送出去. 但是发现还是在自己的密钥也发生了 ...

  6. 错误:selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

    错误再现 原因:firefox浏览器版本和浏览器驱动版本不匹配 解决办法:卸载高版本浏览器,安装低版本浏览器

  7. python-模块安装

    首先到这个网址https://www.lfd.uci.edu/~gohlke/pythonlibs/ 找到自己想要用的模块,然后下载下来, 回到桌面找到文件所在位置进入cmd中, pip instal ...

  8. BlackArch Linux 2019.06.01 宣布发布

    导读 BlackArch Linux是一个基于Arch Linux的发行版,专为渗透测试人员和安全研究人员设计,并包含大量渗透测试和安全实用程序,已宣布发布2019.06.01版本. BlackArc ...

  9. EF Core的安装及入门

    一.环境准备 1.开发环境:.NET Core 3.1 2.IDE工具:Visual Studio 2019 3.数据库:SQL Server 2012 二.EF Core的安装 1.新建一个项目,如 ...

  10. python中__str__与__repr__

    (1)背景 python中,对于类(自定义类)的实例对象的默认显示既没有太大用处,也不美观.比如: class adder: def __init__(self,value=0): self.data ...