struct ComplexListNode{
int data;
ComplexListNode *next;
ComplexListNode *sibling;
};
void CloneNode(ComplexListNode *pHead){
ComplexListNode *pNode=pHead;
while(pNode){
ComplexListNode *pCloneNode=new ComplexListNode();
pCloneNode->data=pNode->data;
pCloneNode->next=pNode->next;
pCloneNode->sibling=NULL;
pNode->next=pCloneNode;
pNode=pNode->next->next;
}
}
void CloneSibling(ComplexListNode *pHead){
ComplexListNode *pNode=pHead;
ComplexListNode *pCloneNode=pHead->next;
while(pNode){
if(pNode->sibling)
pCloneNode->sibling=pNode->sibling->next;
pNode=pNode->next->next;
pCloneNode=pCloneNode->next->next;
}
}
ComplexListNode* Divide(ComplexListNode *pHead){
ComplexListNode *pNode=pHead;
ComplexListNode *pCloneNode=pHead->next;
ComplexListNode *pCloneHead=pHead->next;
while(pNode){
pNode->next=pNode->next->next;
pNode=pNode->next;
if(pCloneNode->next){
pCloneNode->next=pCloneNode->next->next;
pCloneNode=pCloneNode->next;
}
}
return pCloneHead;
}
ComplexListNode* ListClone(ComplexListNode* pHead){
if(pHead==NULL) return NULL;
CloneNode(pHead);
CloneSibling(pHead);
return Divide(pHead);
}