-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyListWithRandonPointer.java
More file actions
133 lines (111 loc) · 3.73 KB
/
CopyListWithRandonPointer.java
File metadata and controls
133 lines (111 loc) · 3.73 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.ArrayList;
public class CopyListWithRandonPointer {
// T(n) = O(n ^ 2)
// Space complexity: O(n)
/** Definition for singly-linked list with a random pointer. */
class RandomListNode {
int label;
RandomListNode next, random;
RandomListNode(int x) { this.label = x; }
}
// T(n) = O(n ^ 2)
// Space complexity: O(n)
// public RandomListNode copyRandomList(RandomListNode head) {
// if (head == null) return null;
//
// RandomListNode copyRoot = new RandomListNode(head.label);
// RandomListNode copyCur = copyRoot;
// RandomListNode oriCur = head;
//
// ArrayList<RandomListNode> oriNodeList = new ArrayList<RandomListNode>();
// ArrayList<RandomListNode> copyNodeList = new ArrayList<RandomListNode>();
//
//
//
// oriNodeList.add(head);
// copyNodeList.add(copyRoot);
// //Copy linkedlist without random reference
// //Add origin list to nodeList.
//
// while (oriCur != null && oriCur.next != null) { //O(n)
//
// oriCur = oriCur.next;
// oriNodeList.add(oriCur); //Add
//
// copyCur.next = new RandomListNode(oriCur.label);
// copyCur = copyCur.next;
// copyNodeList.add(copyCur);
// }
//
//
//
// //Find the index in nodelist of each node's random reference
// for (int i = 0; i < oriNodeList.size(); i++) { //O(n)
// oriCur = oriNodeList.get(i);
// copyCur =copyNodeList.get(i);
//
// if (oriCur.random == null) continue;
// for (int j = 0; j < oriNodeList.size(); j++) { //O(n)
// if (oriCur.random == oriNodeList.get(j)) {
// copyCur.random = copyNodeList.get(j);
// }
// }
// }
//
// return copyRoot;
// }
// T(n) = O(n)
// Space = O(n)
// public RandomListNode copyRandomList(RandomListNode head) {
// HashMap<RandomListNode, RandomListNode> map = new HashMap<>();
// RandomListNode cur = head;
// //Build map
// while (cur != null) {
// map.put(cur, new RandomListNode(cur.label));
// cur = cur.next;
// }
// cur = head;
// //Set next and random
// while (cur != null) {
// map.get(cur).next = map.get(cur.next);
// map.get(cur).random = map.get(cur.random);
// cur = cur.next;
// }
// return map.get(head);
// }
public RandomListNode copyRandomList(RandomListNode head) {
RandomListNode iter = head, next;
// First round: make copy of each node,
// and link them together side-by-side in a single list.
while (iter != null) {
next = iter.next;
RandomListNode copy = new RandomListNode(iter.label);
iter.next = copy;
copy.next = next;
iter = next;
}
// Second round: assign random pointers for the copy nodes.
iter = head;
while (iter != null) {
if (iter.random != null) {
iter.next.random = iter.random.next;
}
iter = iter.next.next;
}
// Third round: restore the original list, and extract the copy list.
iter = head;
RandomListNode pseudoHead = new RandomListNode(0);
RandomListNode copy, copyIter = pseudoHead;
while (iter != null) {
next = iter.next.next;
// extract the copy
copy = iter.next;
copyIter.next = copy;
copyIter = copy;
// restore the original list
iter.next = next;
iter = next;
}
return pseudoHead.next;
}
}