forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy List with Random Pointer.java
More file actions
executable file
·171 lines (151 loc) · 5.47 KB
/
Copy List with Random Pointer.java
File metadata and controls
executable file
·171 lines (151 loc) · 5.47 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
M
1527962398
tags: Hash Table, Linked List
time: O(n)
space: O(1)
deep copy linked list. linked list 上有random pointer to other nodes.
#### HashMap, Linked List
- Basic Implementation of copy linked list:
- use node and dummy to hold new list, 遍历head.next .... null.
- Map 在这里用来: 1. avoid creating same node; 2. return the item if existing
- map 的 key全部是old object, 新的key全部是 newly created object
- 每一步都check map里面有没有head. 没有? 加上
- 每一步都check map里面有没有head.random. 没有? 加上
```
/*
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.
Hide Company Tags Microsoft Uber
Hide Tags Hash Table Linked List
Hide Similar Problems (M) Clone Graph
LeetCode: Hard
*/
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
/*
Iterative through the list.
Use a dummyHead and return dummyHead.next at the end.
In each iteration, check if Head is already exist, or make a new one
* use HashMap<oldNode, newNode> to mark if a node has been visited.
deep copy the random node of head as well.
border case: if head == null, return null
*/
class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return head;
}
//creat node, used to link all nodes
RandomListNode node = new RandomListNode(0);
RandomListNode dummy = node;
//HashMap to mark node
HashMap<RandomListNode, RandomListNode> map = new HashMap<>();
while(head != null) {
//process head. (we already know head!=null)
if (!map.containsKey(head)) {
map.put(head, new RandomListNode(head.label));
}
node.next = map.get(head);
//process head.random
if (head.random != null) {
if(!map.containsKey(head.random)) {
map.put(head.random, new RandomListNode(head.random.label));
}
node.next.random = map.get(head.random);
}
node = node.next;
head = head.next;
}
return dummy.next;
}
}
/*
Thinking process:
1. Loop through the original list
2. Use a HashMap<old node, new node>. User the old node as a key and new node as value.
3. Doesn't matter of the order of node that being added into the hashMap.
For example, node1 is added.
node1.random, which is node 99, will be added into hashMap right after node1.
4. During the loop:
If head exist in hashmap, get it; if not existed, create new node using head, add into hashMap
If head.random exist, get it; if not, add a new node using head.random.
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return null;
}
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode dummy = new RandomListNode(0);
RandomListNode pre = dummy;
RandomListNode newNode;
while (head != null) {
//Add new node
if (map.containsKey(head)) {
newNode = map.get(head);
} else {
newNode = new RandomListNode(head.label);
map.put(head, newNode);
}
//Add new node's random node
if (head.random != null) {
if (map.containsKey(head.random)) {
newNode.random = map.get(head.random);
} else {
newNode.random = new RandomListNode(head.random.label);
map.put(head.random, newNode.random);
}
}
//append and shift
pre.next = newNode;
pre = newNode;
head = head.next;
}
return dummy.next;
}
}
//Same soluton as above, but split populating map && deep copy, which is not as efficient as above
//Save all possible nodes into HashMap<oldNodeAddress, newNodeAddress>
//Deep copy the list, before adding any node, check map
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return null;
}
//Populate map
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode node = head;
RandomListNode newNode;
while(node != null) {
if (!map.containsKey(node)) {
newNode = new RandomListNode(node.label);
map.put(node, newNode);
}
if (node.random != null && !map.containsKey(node.random)) {
newNode = new RandomListNode(node.random.label);
map.put(node.random, newNode);
}
node = node.next;
}
//Deep copy
node = head;
newNode = new RandomListNode(0);
RandomListNode dummy = newNode;
while (node != null) {
newNode.next = map.get(node);
if (node.random != null)
newNode.next.random = map.get(node.random);
newNode = newNode.next;
node = node.next;
}
return dummy.next;
}
}
```