We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 29fb6f4 commit afb94e9Copy full SHA for afb94e9
cpp/0138-copy-list-with-random-pointer.cpp
@@ -55,7 +55,7 @@ class Node {
55
// return visited[node];
56
// }
57
// };
58
-
+/*
59
class Solution {
60
public:
61
Node* copyRandomList(Node* head) {
@@ -99,3 +99,25 @@ class Solution {
99
return oldHead;
100
}
101
};
102
+*/
103
+
104
+class Solution {
105
+public:
106
+ Node* copyRandomList(Node* head) {
107
+ unordered_map<Node*, Node*> nodes;
108
+ Node* h = head;
109
110
+ while (h){
111
+ nodes[h] = new Node(h->val);
112
+ h = h->next;
113
+ }
114
+ h = head;
115
116
+ Node* newNode = nodes[h];
117
+ newNode->next = nodes[h->next];
118
+ newNode->random = nodes[h->random];
119
120
121
+ return nodes[head];
122
123
+};
0 commit comments