
#include <iostream>
using namespace std;
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==NULL)
return NULL;
RandomListNode* cur=pHead;
while(cur)
{
RandomListNode* NewNode=new RandomListNode(cur->label);
NewNode->next=cur->next;
cur->next=NewNode;
cur=cur->next->next;
}
cur=pHead;
while(cur)
{
RandomListNode* Next=cur->next;
if(cur->random)
Next->random=cur->random->next;
else
Next->random=NULL;
cur=cur->next->next;
}
cur=pHead;
RandomListNode* head2=cur->next;
while(cur)
{
RandomListNode* Next=cur->next;
cur->next=Next->next;
if(cur->next)
Next->next=cur->next->next;
else
Next->next=NULL;
cur=cur->next;
}
return head2;
}
};