题目:
代码(首刷看解析 2024年3月5日):
/*
// 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) {
unordered_map<Node*, Node*> OriMap;
Node* cur = head;
while (cur) {
OriMap[cur] = new Node(cur->val);
cur = cur->next;
}
cur = head;
while (cur) {
OriMap[cur]->next = OriMap[cur->next];
OriMap[cur]->random = OriMap[cur->random];
cur = cur->next;
}
return OriMap[head];
}
};