/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
unordered_map<RandomListNode *, RandomListNode *> m;
return getCopyNode(head, m);
}
private:
RandomListNode * getCopyNode(RandomListNode *head, unordered_map<RandomListNode*, RandomListNode*>& m) {
if (!head) {
return NULL;
}
if (m.find(head) != m.end()) {
return m[head];
}
RandomListNode * node = new RandomListNode(head->label);
m[head] = node;
node->next = getCopyNode(head->next, m);
node->random = getCopyNode(head->random, m);
return node;
}
};