参考:https://www.cnblogs.com/edisonchou/p/4790090.html
https://www.nowcoder.com/questionTerminal/f836b2c43afc4b35ad6adc41ec941dba
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
if(!pHead)
return NULL;
RandomListNode *pCur =pHead;
while(pCur!=NULL){
RandomListNode* node =new RandomListNode(pCur->label);
node->next=pCur->next;
pCur->next=node;
pCur=node->next;
}
pCur=pHead;
while(pCur!=NULL){
if(pCur->random!=NULL){
pCur->next->random=pCur->random->next;
}
pCur=pCur->next->next;
}
pCur=pHead;
RandomListNode *head=pHead->next;
RandomListNode *cur=head;
while(pCur!=NULL){
pCur->next=pCur->next->next;
if(cur->next!=NULL)
cur->next=cur->next->next;
pCur=pCur->next;
cur=cur->next;
}
return head;
}
};