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. 18.swoole学习笔记--案例

    <?php //创建webSocket服务器 $ws=); //open $ws->on('open',function($ws,$request){ echo "新用户 $re ...

  2. 三十一、SAP中的循环和判断图标和表格的混用

    一.代码如下 二.显示结果如下

  3. 047-PHP数字前面补零,固定位数补0

    <?php #PHP 数字前面补零 固定位数补0 $num=128; $num=str_pad($num,8,"0",STR_PAD_LEFT); echo $num; // ...

  4. 操作系统类型&操作系统结构&现代操作系统基本特征

    五大类型操作系统 (1). 批处理操作系统 用户脱机使用计算机 用户提交作业之后直到获得结果之前就不再和计算机打交道. 作业提交的方式可以是直接交给计算中心的管理操作员,也可以是通过远程通讯线路提交. ...

  5. js 混合构造原型 运用

    1.给10个div添加点击事件 <body> <div class="sd">sdss</div> <div class="sd ...

  6. SpringBoot通过ApplicationArguments获取args

    如果你需要获取通过SpringApplication.run(…​)传输过来的arguments,可以直接注入一个ApplicationArguments即可实现,如下面这个例子: @Service ...

  7. html特殊字符的写法

    符号 写法 (空格)   <(小于号) < >(大于号) > " " ®(已注册) ® ©(版权) © ™(商标) ™ (半方大的空白)   (全方大的空白 ...

  8. ping.sh

    扫描整个网段  nmap -sP 10.0.0.0/24 #!/bin/bash ps () { ping $1 -c 3 -w 2 |grep -q "ttl"      #结果 ...

  9. Mac OS 终端 iTerm2配置大全

    转载链接:https://www.cnblogs.com/diyxiaoshitou/p/9017413.html,在按照原文执行时发现有些问题,所以本文对原文中存在问题的地方做了些调整. 之前一直使 ...

  10. idea中maven下载jar包不完整问题

    解决: 1.输入 mvn -U idea:idea 等1.都下载完毕后,在点击2.即Reimport