-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyListwithRandomPointer.java
More file actions
62 lines (58 loc) · 2.06 KB
/
Copy pathCopyListwithRandomPointer.java
File metadata and controls
62 lines (58 loc) · 2.06 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
//1.克隆一个链表
//2.新链表的节点的随机指针都指向原节点
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
//copy without random pointer
RandomListNode dummy = new RandomListNode(0);
RandomListNode anotherDummy = new RandomListNode(0);
dummy.next = head;
RandomListNode tempA = dummy;
RandomListNode tempB = anotherDummy;
while (tempA.next!=null) {
tempB.next = new RandomListNode(tempA.next.label);
tempA = tempA.next;
tempB = tempB.next;
}
HashMap<RandomListNode, RandomListNode> mapAtoB = new HashMap<RandomListNode,RandomListNode>();
tempA = dummy;
tempB = anotherDummy;
while(tempA.next!=null){
mapAtoB.put(tempA.next,tempB.next);
tempA = tempA.next;
tempB = tempB.next;
}
//k=random,value=list of the start dots
HashMap<RandomListNode,List<RandomListNode>> randomA = new HashMap<RandomListNode,List<RandomListNode>>();
tempA = dummy;
while(tempA.next!=null){
List<RandomListNode> needToBeAdded = randomA.getOrDefault(tempA.next.random, new ArrayList<RandomListNode>());
needToBeAdded.add(tempA.next);
randomA.put(tempA.next.random,needToBeAdded);
tempA = tempA.next;
}
tempA = dummy;
tempB = anotherDummy;
List<RandomListNode> randomList;
while(tempB.next!=null){
//all nodes in A that point to tampA.next
randomList = randomA.get(tempA.next);
if (null!=randomList) {
for (RandomListNode each:randomList ) {
//mapping from A to B
mapAtoB.get(each).random = tempB.next;
}
}
tempA = tempA.next;
tempB = tempB.next;
}
return anotherDummy.next;
}
}