Java for LeetCode 138 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
解题思路:
我们在Java for LeetCode 133 Clone Graph题中做过图的复制,本题和图的复制十分类似,JAVA实现如下:
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null)
return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode node = new RandomListNode(head.label);
RandomListNode headTemp = head, nodeTemp = node;
map.put(head, node);
while (headTemp.next != null) {
nodeTemp.next = new RandomListNode(headTemp.next.label);
map.put(headTemp.next, nodeTemp.next);
headTemp = headTemp.next;
nodeTemp = nodeTemp.next;
}
headTemp = head;
nodeTemp = node;
while (headTemp!= null) {
if(map.containsKey(headTemp.random))
nodeTemp.random=map.get(headTemp.random);
headTemp = headTemp.next;
nodeTemp = nodeTemp.next;
}
return node;
}
Java for LeetCode 138 Copy List with Random Pointer的更多相关文章
- leetcode 138. Copy List with Random Pointer ----- java
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
- Leetcode#138 Copy List with Random Pointer
原题地址 非常巧妙的方法,不需要用map,只需要O(1)的额外存储空间,分为3步: 1. 先复制链表,但是这个复制比较特殊,每个新复制的节点添加在原节点的后面,相当于"加塞"2. ...
- [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表
public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- 138. Copy List with Random Pointer (not do it by myself)
A linked list is given such that each node contains an additional random pointer which could point t ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
随机推荐
- Android Gradle 经验总结
用过android studio的对gradle应该都不陌生了,gradle文件的基本配置大同小异,略做了解使用应该是没什么问题了.但是深入细致的了解一下对于理解项目还是很有帮助的,尤其是遇到一些配置 ...
- python super 的正确理解
http://www.jb51.net/article/87807.htm super指的是MRO中的下一个类
- 搭建k8s集群的手顺
https://www.cnblogs.com/netsa/category/1137187.html
- rsync一些常用的命令
渗透测试的时候会遇到RSYNC 匿名访问 在对一些大型互联网进行测试的时候经常会遇到rsync. 什么是Rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LA ...
- linux挂载硬盘
最近有个生产机由于缓存问题,硬盘不太够用,所以就加载了一块硬盘,下面把挂载硬盘的过程记录下,供大家参考,有问题欢迎留言! fdisk –l ------------------------------ ...
- Nginx配置文档具体解释
Nginx的配置文档具体解释.在这儿做个总结,以便以后使用的时间查看. 下面大部分自己整理.部分来自參考 #设置用户 #user nobody; #启动进程数(一般和server的CPU同样) #能 ...
- metaq入门部署到实战
初识metaq zookeeper部署,这里单机zk为例. wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.5/zookeep ...
- ie 浏览器无法保存cookie,且与域名包括了下划线(_)有关系的问题
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- linux下性能监控工具
一. Linux 性能监控的概述 系统由若干子系统构成,通常改动一个子系统有可能影响到另外一个子系统.甚至会导致整个系统不稳定.崩溃. 所以说优化.监測.測试一般是连在一起的,并且是一个循环并且长期 ...
- POJ3009 Curling 2.0(DFS)
迷宫问题求最短路. 略有不同的是假设不碰到石头的话会沿着一个方向一直前进,出界就算输了.碰到石头,前方石头会消失,冰壶停在原地. 把这个当作状态的转移. DFS能够求出其最小操作数. #include ...