forked from walnutown/CodingInTheDeep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyListWithRandomPointer.java
More file actions
35 lines (32 loc) · 1.16 KB
/
Copy pathCopyListWithRandomPointer.java
File metadata and controls
35 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
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.
*/
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
// Maintain a map to memoize the nodes that have been copied
// time: O(n); space:O(n)
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head ==null) return null;
Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode sen = new RandomListNode(0), p = head, s = sen;
while (p != null){
if (!map.containsKey(p))
map.put(p, new RandomListNode(p.label));
if (p.random!=null && !map.containsKey(p.random))
map.put(p.random, new RandomListNode(p.random.label));
s.next = map.get(p);
s = s.next;
s.random = map.get(p.random);
p = p.next;
}
return sen.next;
}
}