Skip to content

Commit

Permalink
Update copy-list-with-random-pointer.cpp as in the video
Browse files Browse the repository at this point in the history
Problem: 0138-copy-list-with-random-pointer.cpp
Language: C++
Submission: https://leetcode.com/problems/copy-list-with-random-pointer/submissions/875731269/
  • Loading branch information
caba5 authored Jan 10, 2023
1 parent 29fb6f4 commit afb94e9
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cpp/0138-copy-list-with-random-pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Node {
// return visited[node];
// }
// };

/*
class Solution {
public:
Node* copyRandomList(Node* head) {
Expand Down Expand Up @@ -99,3 +99,25 @@ class Solution {
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];
}
};

0 comments on commit afb94e9

Please sign in to comment.