Skip to content

Commit afb94e9

Browse files
authored
Update copy-list-with-random-pointer.cpp as in the video
Problem: 0138-copy-list-with-random-pointer.cpp Language: C++ Submission: https://leetcode.com/problems/copy-list-with-random-pointer/submissions/875731269/
1 parent 29fb6f4 commit afb94e9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cpp/0138-copy-list-with-random-pointer.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Node {
5555
// return visited[node];
5656
// }
5757
// };
58-
58+
/*
5959
class Solution {
6060
public:
6161
Node* copyRandomList(Node* head) {
@@ -99,3 +99,25 @@ class Solution {
9999
return oldHead;
100100
}
101101
};
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+
while (h){
116+
Node* newNode = nodes[h];
117+
newNode->next = nodes[h->next];
118+
newNode->random = nodes[h->random];
119+
h = h->next;
120+
}
121+
return nodes[head];
122+
}
123+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy