[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 ...
随机推荐
- JPA#OneToOne
无力吐槽. 一对一,一个人有一个身份证号码.一个人有一条命,类似于这一种的就是一对一的关系. 涉及到的注解两个: OneToOne JoinColumn( name="当前实体对应数据库表中 ...
- springboot#filter
_ Filter不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对HttpServletResponse 进行后处理,是个典型的处理链.完 ...
- 019-PHP创建目录函数
<?php if (mkdir("myDir1", 0777)) //创建目录的函数 { print("目录创建成功"); //目录建立成功 } else ...
- C#的listview
listView1.Items.Clear(); ListViewItem listitem = new ListViewItem(字符串);//这是第一列的内容,需要,而且必须通过构造方法添加 ; ...
- “战疫”需求不再等-京东云与AI【应急资源信息发布平台】召集开发者共同支援
截止北京时间 2020年2月5日19时00分,全国确诊新型冠状病毒肺炎24423例,疑似23260例. 新年伊始,一切都显得和往年有那么一点不一样.疫情牵动着每一个人的心脏,也有很多人早就放弃了假期投 ...
- vue使用Vant UI中的swiper组件及传值
子组件SwiperBanner <!-- --> <template> <div class="swiper"> <van-swipe : ...
- 每日一linux命令
每日一linux命令 http://www.cnblogs.com/peida/tag/每日一linux命令/
- 使用技巧 --- 与VS Code相关
目的:修改VS Code的注释文本颜色 S1:假设VS Code的安装路径是 %MVSC% S2:文件资源管理器进入目录 %MVSC%\resources\app\extensions\ S3:该目录 ...
- ajax 跨域webapi 最简单的demo(只介绍Get)
这几天遇到一个nodejs的项目,使用VSCode开发,需要连接数据库的,但是用nodejs连接数据库比较繁琐,需要安装很多东西,本人也懒得去研究了.后来想到建一个WebAPI然后用ajax来调用,避 ...
- Centos7.4系统 httpd模式搭建文件服务器
环境:服务环境:centos7.4 说明:搭建Apache文件服务器,下载路径为/opt/ymyg(下载路径根据实际需要自己定义) 步骤: 1.安装httpd服务 [root@localhost ...