标签:
#ifndef COMPLEX_LISTCLONE_H
#define COMPLEX_LISTCLONE_H
#include<iostream>
struct ComplexListNode{
int m_nValue;
struct ComplexListNode *m_pNext;
struct ComplexListNode *m_pSibling;
};
ComplexListNode *complexListCloned(ComplexListNode **head){
cloneNodes(head);
connectpSibingNodes(head);
reconnectNodes(head);
}
void cloneNodes(ComplexListNode **head){
ComplexListNode *pNode=*head;
while(pNode!=NULL){
ComplexListNode *pClonedNode=new ComplexListNode();
pClonedNode->m_nValue=pNode->m_nValue;
pClonedNode->m_pNext=pNode->m_pNext;
pNode->m_pNext=pClonedNode;
pClonedNode->m_pSibling=NULL;
pNode=pNode->m_pNext;
}
}
void connectpSibingNodes(ComplexListNode **head){
ComplexListNode *pNode=*head;
ComplexListNode *pCloned=NULL;
while(pNode!=NULL){
if(pNode->m_pSibling!=NULL){
ComplexListNode *pCloned=pNode->m_pNext;
pCloned->m_pSibling=pNode->m_pSibling->m_pNext;
}
pNode=pCloned->m_pNext;
}
}
ComplexListNode* reconnectNodes(ComplexListNode **head){
if(*head==NULL||head==NULL){
return ;
}
ComplexListNode *pNode=*head;
ComplexListNode *cloneRoot=pNode->m_pNext;
ComplexListNode *cloneNode=cloneRoot;
while(pNode!=NULL){
pNode->m_pNext=cloneNode->m_pNext;
pNode=pNode->m_pNext;
cloneNode->m_pNext=pNode->m_pNext;
cloneNode=cloneNode->m_pNext;
}
return cloneRoot;
}
#endif
标签:
原文地址:http://www.cnblogs.com/yml435/p/4655483.html