[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 ...
随机推荐
- WTM框架在开发过程中如何动态迁移表和创建表
官方迁移方法:https://wtmdoc.walkingtec.cn/#/Data/Migration 但是在实际开发过程中使用Add-Migration 方法迁移会发现,把系统内置的表也全部带出来 ...
- 类的始祖Object
一.概述 Object时java中顶级父类,也是唯一没有父类的类:它是整个java中最基本的类,在java中所有的类都默认继承了Object. 二.重要方法 1.clone方法 克隆出一个新的对象. ...
- 201771010142-张燕 实验一 软件工程准备—<软件工程的初步了解和学习目标>
实验一 软件工程准备 项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh/ 软件工程准备要求 https://www.cnblogs.com/nwnu-daiz ...
- assert和hasattr,getattr,setattr
assert hasattr(self, 'initial_data'), ( 'Cannot call `.is_valid()` as no `data=` keyword argument wa ...
- POJ 3253:Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33114 Accepted: 10693 De ...
- C语言预处理理论-宏定义2
宏定义21.带参宏和带参函数的区别(1)宏定义是在预处理期间处理的,而函数是在编译期间处理的.这个区别带来的实质差异是:宏定义最终是在调用宏的地方把宏体原地展开,而函数是在调用函数处跳转到函数中去执行 ...
- 原生js完成打地鼠小游戏
:这是首页,有简单模式和地狱模式两种模式进行选择 这是选择完模式之后的游戏界面:30秒一局游戏倒计时,每打中一只老鼠加一分,没砸中减一分,没砸不加不减 首先准备几张图片 html代码: <!-- ...
- redis主从复制原理与优化-高可用
一 什么是主从复制 机器故障:容量瓶颈:QPS瓶颈 一主一从,一主多从 做读写分离 做数据副本 扩展数据性能 一个maskter可以有多个slave 一个slave只能有一个master 数据流向是单 ...
- 2020/2/5 php编程学习
一事无成的上午..就安了框架解决了一些报错信息 下面简单了解一下 安装一下框架: 什么是路由: 系统从URI(唯一资源定位器)参数中分析出当前请求的分组(平台),控制器和操作方法的过程就是路由. UR ...
- POJ 2993:Emag eht htiw Em Pleh
Emag eht htiw Em Pleh Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64 ...