题目链接:https://leetcode.com/problems/copy-list-with-random-pointer/tabs/description
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
class Solution {
private:
unordered_map<RandomListNode*, RandomListNode*> hmap;
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head) return NULL;
if (hmap.find(head) != hmap.end())
return hmap.find(head)->second;
RandomListNode* node = new RandomListNode(head->label);
hmap[head] = node;
node->next = copyRandomList(head->next);
node->random = copyRandomList(head->random);
return node;
}
};