Copy List with Random Pointer
2014-01-28 00:17
513 查看
Copy List with Random Pointer
Total Accepted: 5185 TotalSubmissions: 25214My Submissions
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.
原理用一个hashmap<original node, cloned node> 来保存 原始node 和 克隆node 的映射。 第一种方法可以设置一个dummy head, 然后遍历原始的list, 只需要克隆原始node和 random pointer就可以了,next由dummy head 这个list去手动连接。 第二种是所有的东西一起克隆 (原始node, random pointer, next pointer), 这里就不需要dummy head了。直接克隆一遍就可以了。 |
public static RandomListNode copyRandomList(RandomListNode head){
if(head == null) return null;
Map<RandomListNode,RandomListNode> map = new HashMap<>();
RandomListNode dummyHead = new RandomListNode(0);
RandomListNode temp = dummyHead;
while(head!=null){
// copy node
RandomListNode cloned = null;
if(map.containsKey(head)){
cloned = map.get(head);
}else{
cloned = new RandomListNode(head.label);
map.put(head,cloned);
}
temp.next = cloned;
// copy node's random
if(map.containsKey(head.random)){
cloned.random = map.get(head.random);
}else if(head.random!=null){
RandomListNode random = new RandomListNode(head.random.label);
cloned.random = random;
map.put(head.random, random);
}
head = head.next;
temp = temp.next; // node's next is connected by dummy
}
return dummyHead.next;
}
// method 2: clone node, next, and random pointer.
public static RandomListNode copyRandomList2(RandomListNode head){
if(head == null) return null;
Map<RandomListNode,RandomListNode> map = new HashMap<>();
RandomListNode clonedHead = null;
while(head!=null){
// copy node
RandomListNode cloned = null;
if(map.containsKey(head)){
cloned = map.get(head);
}else{
cloned = new RandomListNode(head.label);
map.put(head,cloned);
}
// copy node's next
if(map.containsKey(head.next)){
cloned.next = map.get(head.next);
}else if(head.next!=null){
RandomListNode node = new RandomListNode(head.next.label);
cloned.next = node;
map.put(head.next,node);
}
// copy node's random
if(map.containsKey(head.random)){
cloned.random = map.get(head.random);
}else if(head.random!=null){
RandomListNode random = new RandomListNode(head.random.label);
cloned.random = random;
map.put(head.random, random);
}
if(clonedHead == null) clonedHead = cloned;
head = head.next;
}
return clonedHead;
}
相关文章推荐
- 【LeetCode】Copy List with Random Pointer
- leetcode第一刷_Copy List with Random Pointer
- leetcode -day8 Copy List with Random Pointer & Single Number I II
- leetcode Copy List with Random Pointer
- 【LeetCode】Copy List with Random Pointer
- Copy List with Random Pointer
- LeetCode OJ - Copy List with Random Pointer
- Copy List with Random Pointer
- leetcode -- Copy List with Random Pointer -- deep copy问题,dfs
- Copy List with Random Pointer
- [LeetCode]Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- Copy List With Random Pointer
- leetcode - Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode Copy List with Random Pointer
- LintCode - Copy List with Random Pointer
- Leetcode--Copy List with Random Pointer
- LeetCode Copy List with Random Pointer 分析解答
- copy-list-with-random-pointer