/**
* 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) {
if (head == NULL) return NULL;
RandomListNode *nHead = new RandomListNode(head->label);
RandomListNode *p = head, *q = nHead;
unordered_map<RandomListNode*, RandomListNode*> hash;
hash[head] = nHead;
while (p->next) {
RandomListNode *tmp = new RandomListNode(p->next->label);
hash[p->next] = tmp;
q->next = tmp, p = p->next, q = q->next;
}
p = head;
while(p) {
hash[p]->random = hash[p->random];
p = p->next;
}
return nHead;
}
};