Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitafb94e9

Browse files
authored
Update copy-list-with-random-pointer.cpp as in the video
Problem: 0138-copy-list-with-random-pointer.cppLanguage: C++Submission:https://leetcode.com/problems/copy-list-with-random-pointer/submissions/875731269/
1 parent29fb6f4 commitafb94e9

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+
classSolution {
105+
public:
106+
Node*copyRandomList(Node* head) {
107+
unordered_map<Node*, Node*> nodes;
108+
Node* h = head;
109+
110+
while (h){
111+
nodes[h] =newNode(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)

[8]ページ先頭

©2009-2025 Movatter.jp