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. 【踩坑记录】记录一次使用Python logging库多进程打印日志的填坑过程

    背景: 项目使用Python自带的logging库来打印日志 项目部署在一台Centos7的机器上 项目采用gunicorn多进程部署 过程: 1.LOG日志代码封装: 采用logging库,并设置w ...

  2. 二十一、SAP中通过内表输出数据库中数据

    一.我们查看一个SCARR的一个数据库 二.数据库内容如下 三.我们写一个关于内表使用的代码,来显示这个数据库内容 四.输出如下

  3. 035-PHP简单定义一个闭包函数

    <?php /* + 什么是闭包函数?即一个函数内部,包含了1-N个匿名函数, + 用处是可以做局部数据缓存与实现封装(有点类似class) */ # 函数内部,定义一个匿名函数,即可称为闭包函 ...

  4. Spark笔记(一)

    简介 Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎.Spark是UC Berkeley AMP lab (加州大学伯克利分校的AMP实验室)所开源的类Hadoop MapR ...

  5. Node.js 发送Email

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  6. 【LeetCode】206. 反转链表

    题目 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: 你可 ...

  7. 关于Promise的异步依次函数调用

    在Promise中async用于定义一个异步函数(可不写),该函数返回一个Promise. 如果async函数返回的是一个同步的值,这个值将被包装成一个理解resolve的Promise, 等同于re ...

  8. ssi引用路径规则

    1.file:引用的是相对路径,相对当前文件的,特别注意使用file,被引用文件只能与当前文件同级.或下级.不能在其上级(不支持../) <!--#include file="head ...

  9. cf 444C.

    听说这是线段树的裸题??(看来我也就能搞个求和什么的了2333) #include<bits/stdc++.h> #define INF 0x7fffffff #define LL lon ...

  10. cf442 B.Andrey and Problem

    看题偷瞄到题解2333(以为是劲题呢..结果是乱贪心,奇怪) 排序之后,如果加入下一个比现在更优就更新答案(奇怪啊) t=ans*(1-a[i])+s*a[i];(ans*(1-a[i])是新的一位不 ...