Created
November 26, 2014 09:59
-
-
Save rohith2506/2040f6af4f1835834c74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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