[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 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的更多相关文章
- [Algo] 132. Deep Copy Undirected Graph
Make a deep copy of an undirected graph, there could be cycles in the original graph. Assumptions Th ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- [Linked List]Copy List with Random Pointer
Total Accepted: 53943 Total Submissions: 209664 Difficulty: Hard A linked list is given such that ea ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【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 ...
- Copy List with Random Pointer [LeetCode]
A linked list is given such that each node contains an additional random pointer which could point t ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
随机推荐
- 133-PHP子类无法重写父类private同名函数
<?php class father{ //定义father类 //定义protected成员方法 protected function cook(){ return 'protected co ...
- C#不显示在任务栏
在我用c#写一些小程序是总是希望,程序窗体不在任务栏上显示程序的窗体,c# Form提供了一个 属性值可以很好的解决这个问题 这个属性就是 ShowInTaskbar 在微软的官方声明格式为: pub ...
- eclipse 下配置安卓环境
建议你看博客 http://blog.csdn.net/sinat_21184471/article/details/76131141 其中一些细节问题,我会根据我犯过的错误说明一下的!!!! 它 ...
- SciKit-Learn 数据集基本信息
## 保留版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Le ...
- P1013 数素数
转跳点:
- 2018出炉50道iOS面试题
基础: 1.如何令自己所写的对象具有拷贝功能? 若想令自己所写的对象具有拷贝功能,则需实现 NSCopying 协议.如果自定义的对象分为可变版本与不可变版本,那么就要同时实现 NSCopying与 ...
- javascript设计模式(1)——面向对象基础
用对象收编变量2种方式 1 函数式 var Object = { name:function(){ return this; }, email:function(){ return this; } } ...
- 量化投资_Multicharts数组操作函数_zeros()设定数组元素为0(自定义)
1. 函数的用法类似于Python的zeros函数,给定数组尺寸,让数组的元素归零 //zeros:根据设定的尺寸长度,让一维数组的元素全部归零 inputs: arr[MaxSize]( numer ...
- QSignalMapper is deprecated
今天参考 qt4 的书籍,在 qt5 的平台上面,用了 QSignalMapper,结果收到警告" QSignalMapper is deprecated". 经过一番查找,找到了 ...
- css笔记01
CSS样式(Cascading Style Sheets) 表格布局缺陷: 嵌套太多,一旦顺序错乱页面达不到预期效果 表格布局页面不灵活,动一块整个布局全都要变 语法: 在style标签中 ...