class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
if(pHead == NULL)
return NULL;
RandomListNode* currentNode = pHead;
while(currentNode){
RandomListNode *cloneNode = new RandomListNode(currentNode->label);
RandomListNode* nextNode = currentNode->next;
currentNode->next = cloneNode;
cloneNode->next = nextNode;
currentNode = nextNode;
}
currentNode = pHead;
while(currentNode && currentNode->next){
currentNode->next->random = currentNode->random==NULL?NULL:currentNode->random->next;
currentNode = currentNode->next->next;
}
currentNode = pHead;
RandomListNode *pCloneHead = pHead->next;
while(currentNode){
RandomListNode* cloneNode = currentNode->next;
currentNode->next = cloneNode->next;
cloneNode->next = cloneNode->next==NULL?NULL:cloneNode->next->next;
currentNode = currentNode->next;
}
return pCloneHead;
}
};