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 to any node in the list or null.
Return a deep copy of the list.
和第133题差不多,都是图的复制,区别在于这道题的label有可能是相同的,所以导致了map的key有可能相同,所以需要处理。
两种方法差不多。第二种更简洁。
1、在复制next之后修改原结构的label为顺序增长,方便建立map,之后再修改回来。
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if( head == null )
return null;
Map map2 = new HashMap<Integer,RandomListNode>();
int num = 1; RandomListNode node = head;
RandomListNode newNode = new RandomListNode(head.label);
RandomListNode node2 = newNode;
map2.put(0,node2);
node.label = 0; node = node.next;
while( node != null ){
RandomListNode nextNode = new RandomListNode(node.label);
node2.next = nextNode;
node2 = node2.next;
node.label = num;
map2.put(num,node2);
num++;
node = node.next; } node = head;
node2 = newNode; while( node != null ){ if( node.random == null){
node = node.next;
node2 = node2.next;
continue;
} node2.random = (RandomListNode) map2.get( node.random.label ); node = node.next;
node2 = node2.next; }
node2 = newNode;
node = head;
while( node != null ){
node.label = node2.label;
node = node.next;
node2 = node2.next;
} return newNode; }
}
2、建立map的时候使用
Map<RandomListNode,RandomListNode>
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) { if( head == null )
return null;
Map<RandomListNode,RandomListNode> map = new HashMap<RandomListNode,RandomListNode>(); RandomListNode node = head;
while( node != null ){
map.put(node,new RandomListNode(node.label));
node = node.next;
}
node = head;
while( node != null ){ map.get(node).next = map.get(node.next);
map.get(node).random = map.get(node.random);
node = node.next; }
return map.get(head); }
}
leetcode 138. Copy List with Random Pointer ----- java的更多相关文章
- 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 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 ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- web安全测试-AppScan
安全测试应该是测试中非常重要的一部分,但他常常最容易被忽视掉. 尽管国内经常出现各种安全事件,但没有真正的引起人们的注意.不管是开发还是测试都不太关注产品的安全.当然,这也不能怪我们苦B的“民工兄弟” ...
- IPAD2 5.1.1越狱后的屏幕不能自动旋转~~~
己顶,出现这问题的原因是因为越狱安装了插件的原因.问题解决了,大家没有遇到类似的问题吗?问题出在大家都装了一个SBSettings的插件,解决办法就是在这个插件的ISO 5+ Notification ...
- matlab函数调用及数据传递
http://www.cnblogs.com/duanp/archive/2008/11/29/Matlab-GUIDE.html函数调用 在一个m文件中,可以定义多个函数,但是文件名一定要与第一个函 ...
- AutoCAD ObjectARX(VC)开发基础与实例教程2014版光盘镜像
AutoCAD ObjectARX(VC)开发基础与实例教程2014,最新版,光盘镜像 作者:张帆 朱文俊 编著 出版社:中国电力出版社 出版时间:2014年6月 点击一下
- char、nvarchar和varchar区别
这3种字符串数据类型是我们使用最多的,我们在数据库设计时到底该怎么使用了?首先我们先来分析3个数据类型的说明: 1.char CHAR的长度是固定的,最长2000个字符. 2.varchar 和 va ...
- javascript正则表达式替换字符串
var reg = /^per_list(.*)[\d]{1,}(.*)/;var str = "per_listAmtApril1.value";var replaceStr = ...
- IOS @2X.png
[UIImage imageNamed:@"xxx.png"] 或者xib里iPhone4会自动找*@2x.png initWithContentOfFile:pathToImag ...
- 自动获取UILabel的宽度高度
在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需要的长度 CGSize titleSize = [aSt ...
- Effective C++ ----以对象管理资源
以对象管理资源 通过对象的析构函数的自动调用来自动释放资源 第一部分:几种典型的以对象管理资源的例子 1. STL::auto_ptr 获取资源后立刻放入资源管理对象 std::auto_ptr< ...
- Python 的property的实现 .
描述符.就是 将某种特殊类型①的类的实例指派给另一个类的属性 ①只要实现一下三种方法的其中一个就是特殊类型. __get__(self,instance,owner) -用于访问属性,他返回属性的值. ...