Copy List with Random Pointer leetcode 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.
题解:
如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描list。
第一种方法,就是使用HashMap来坐,HashMap的key存原始pointer,value存新的pointer。
第一遍,先不copy random的值,只copy数值建立好新的链表。并把新旧pointer存在HashMap中。
第二遍,遍历旧表,复制random的值,因为第一遍已经把链表复制好了并且也存在HashMap里了,所以只需从HashMap中,把当前旧的node.random作为key值,得到新的value的值,并把其赋给新node.random就好。
代码如下:
1 public RandomListNode copyRandomList(RandomListNode head) {
2 if(head==null)
3 return null;
4 HashMap<RandomListNode,RandomListNode> map = new HashMap<RandomListNode,RandomListNode>();
5 RandomListNode newhead = new RandomListNode(head.label);
6 map.put(head,newhead);
7 RandomListNode oldp = head.next;
8 RandomListNode newp = newhead;
9 while(oldp!=null){
RandomListNode newnode = new RandomListNode(oldp.label);
map.put(oldp,newnode);
newp.next = newnode;
oldp = oldp.next;
newp = newp.next;
}
oldp = head;
newp = newhead;
while(oldp!=null){
newp.random = map.get(oldp.random);
oldp = oldp.next;
newp = newp.next;
}
return newhead;
}
上面那种方法遍历2次list,所以时间复杂度是O(2n)=O(n),然后使用了HashMap,所以空间复杂度是O(n)。
第二种方法不使用HashMap来做,使空间复杂度降为O(1),不过需要3次遍历list,时间复杂度为O(3n)=O(n)。
第一遍,对每个node进行复制,并插入其原始node的后面,新旧交替,变成重复链表。如:原始:1->2->3->null,复制后:1->1->2->2->3->3->null
第二遍,遍历每个旧node,把旧node的random的复制给新node的random,因为链表已经是新旧交替的。所以复制方法为:
node.next.random = node.random.next
前面是说旧node的next的random,就是新node的random,后面是旧node的random的next,正好是新node,是从旧random复制来的。
第三遍,则是把新旧两个表拆开,返回新的表即可。
代码如下:
1 public RandomListNode copyRandomList(RandomListNode head) {
2 if(head == null)
3 return head;
4 RandomListNode node = head;
5 while(node!=null){
6 RandomListNode newNode = new RandomListNode(node.label);
7 newNode.next = node.next;
8 node.next = newNode;
9 node = newNode.next;
}
node = head;
while(node!=null){
if(node.random != null)
node.next.random = node.random.next;
node = node.next.next;
}
RandomListNode newHead = head.next;
node = head;
while(node != null){
RandomListNode newNode = node.next;
node.next = newNode.next;
if(newNode.next!=null)
newNode.next = newNode.next.next;
node = node.next;
}
return newHead;
}
Reference:http://blog.csdn.net/linhuanmars/article/details/22463599
Copy List with Random Pointer leetcode java的更多相关文章
- Copy List with Random Pointer [LeetCode]
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- ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 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 ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- 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 ----- 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 ...
随机推荐
- 使用GNU工具链进行嵌入式裸机开发
Embedded-Programming-with-the-GNU-Toolchain Vijay Kumar B. vijaykumar@bravegnu.org 翻译整理:thammer gith ...
- Java 异常处理之 论 finally块何时候不走
一. exit退出异常: import java.util.Scanner; public class Test3exit { /** * @param 房山的猫 * finally什么时候不走 * ...
- Linux驱动之PCI
<背景> PCI设备有许多地址配置的寄存器,初始化时这寄存器来配置设备的总线地址,配置好后CPU就可以访问该设备的各项资源了.(提炼:配置总线地址) <配置寄存器> ( ...
- bootm命令移植
<bootm作用> 为linux内核的启动准备条件 <bootloader作用总结> (1)初始化软/硬件(内存硬件/外部设备/堆栈) (2)启动操作系统 <uImagi ...
- PHP 笔记——自定义函数
1. 定义函数 function function_name ([$arg_1],[$arg_2], ... [$arg_n]){ fun_body; [return arg_n;] } 在PHP中, ...
- [BZOJ4567][SCOI2016]背单词(Trie+贪心)
1.题意表述十分难以理解,简单说就是:有n个单词,确定一个背的顺序,使总代价最小. 2.因为第(1)种情况的代价是n*n,这个代价比任何一种不出现第(1)种情况的方案都要大,所以最后肯定不会出现“背某 ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- hdu 1325 判断有向图是否为树
题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...
- IE7 css兼容问题
1,float:right; 在IE错位问题 : 使用position:absolute:right:0px; 2,汉字在float状态下 折行 ,可能是因为父级宽度不够, 改用 display:in ...
- Maven学习-使用Nexus搭建Maven私服
为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到 ...