Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created November 26, 2014 09:59
Show Gist options
  • Save rohith2506/2040f6af4f1835834c74 to your computer and use it in GitHub Desktop.
Save rohith2506/2040f6af4f1835834c74 to your computer and use it in GitHub Desktop.
/**
* 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 *hh = new RandomListNode(head->label);
RandomListNode *cur = head;
RandomListNode *ret = hh;
unordered_map<RandomListNode *, RandomListNode *> pmap;
pmap[cur] = hh;
while(cur -> next){
cur = cur -> next;
hh -> next = new RandomListNode(cur->label);
hh = hh -> next;
pmap[cur] = hh;
}
cur = head;
while(cur){
if(pmap.count(cur)){
hh = pmap[cur];
if(cur -> random && pmap.count(cur -> random)){
hh -> random = pmap[cur -> random];
}
}
cur = cur -> next;
}
return ret;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment