-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0138-copy-list-with-random-pointer.cpp
144 lines (129 loc) · 3.49 KB
/
0138-copy-list-with-random-pointer.cpp
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
/*
Given linked list w/ also a random pointer, construct deep copy
Hash map {old -> new}, O(n) space
Optimize interweave old and new nodes, O(1) space
A -> A' -> B -> B' -> C -> C', A'.random = A.random.next
Time: O(n)
Space: O(n) -> can optimize to O(1)
*/
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
// class Solution {
// public:
// Node* copyRandomList(Node* head) {
// if (head == NULL) {
// return NULL;
// }
// Node* oldNode = head;
// Node* newNode = new Node(oldNode->val);
// visited[oldNode] = newNode;
// while (oldNode != NULL) {
// newNode->next = getClonedNode(oldNode->next);
// newNode->random = getClonedNode(oldNode->random);
// oldNode = oldNode->next;
// newNode = newNode->next;
// }
// return visited[head];
// }
// private:
// unordered_map<Node*, Node*> visited;
// Node* getClonedNode(Node* node) {
// if (node == NULL) {
// return NULL;
// }
// if (visited.find(node) != visited.end()) {
// return visited[node];
// }
// visited[node] = new Node(node->val);
// return visited[node];
// }
// };
/*
class Solution {
public:
Node* copyRandomList(Node* head) {
if (head == NULL) {
return NULL;
}
Node* ptr = head;
while (ptr != NULL) {
Node* newNode = new Node(ptr->val);
newNode->next = ptr->next;
ptr->next = newNode;
ptr = newNode->next;
}
ptr = head;
while (ptr != NULL) {
if (ptr->random == NULL) {
ptr->next->random == NULL;
} else {
ptr->next->random = ptr->random->next;
}
ptr = ptr->next->next;
}
Node* oldPtr = head;
Node* newPtr = head->next;
Node* oldHead = head->next;
while (oldPtr != NULL) {
oldPtr->next = oldPtr->next->next;
if (newPtr->next == NULL) {
newPtr->next = NULL;
} else {
newPtr->next = newPtr->next->next;
}
oldPtr = oldPtr->next;
newPtr = newPtr->next;
}
return oldHead;
}
};
*/
// class Solution {
// public:
// Node* copyRandomList(Node* head) {
// unordered_map<Node*, Node*> nodes;
// Node* h = head;
// while (h){
// nodes[h] = new Node(h->val);
// h = h->next;
// }
// h = head;
// while (h){
// Node* newNode = nodes[h];
// newNode->next = nodes[h->next];
// newNode->random = nodes[h->random];
// h = h->next;
// }
// return nodes[head];
// }
// };
class Solution {
public:
Node* copyRandomList(Node* head) {
unordered_map<Node*, Node*> nodes;
Node* curr = head;
while (curr != NULL) {
nodes[curr] = new Node(curr->val);
curr = curr->next;
}
curr = head;
while (curr != NULL) {
nodes[curr]->next = nodes[curr->next];
nodes[curr]->random = nodes[curr->random];
curr = curr->next;
}
return nodes[head];
}
};