题目链接:
https://leetcode.cn/problems/fu-za-lian-biao-de-fu-zhi-lcof/description/?favorite=xb9nqhhg&orderBy=hot
代码:
class Solution {
public:
map<Node*,Node*>mp;
Node* copyRandomList(Node* head) {
if(head==NULL)
{
return NULL;
}
Node *cnt=head;
while(cnt!=NULL)
{
mp[cnt]=new Node(cnt->val);
cnt=cnt->next;
}
cnt=head;
while(cnt!=NULL)
{
mp[cnt]->next=mp[cnt->next];
mp[cnt]->random=mp[cnt->random];
cnt=cnt->next;
}
return mp[head];
}
};