struct ComplexListNode
{
int m_val;
ComplexListNode* m_pNext;
ComplexListNode* m_pSibling;
};
void CloneNodes(ComplexListNode* pHead)
{
ComplexListNode* pNode = pHead;
while(pNode != NULL)
{
ComplexListNode* pCloned = new ComplexListNode();
pCloned->m_val = pNode->m_val;
pCloned->m_pNext = pNode->m_pNext;
pCloned->m_pSibling = NULL;
pNode->m_pNext = pCloned;
pNode = pCloned->m_pNext;
}
}
void ConnectsiblingNodes(ComplexListNode* pHead)
{
ComplexListNode* pNode = pHead;
while(pNode != NULL)
{
ComplexListNode* pClonded = pNode->m_pNext;
if(pClonded != NULL)
{
pClonded->m_pSibling = pNode->m_pSibling->m_pNext;
}
pNode = pClonded->m_pNext;
}
}
ComplexListNode* ReconnectNodes(ComplexListNode* pHead)
{
ComplexListNode* pNode = pHead;
ComplexListNode* pCloneHead = NULL;
ComplexListNode* pCloneNode = NULL;
if(pNode != NULL)
{
pCloneHead = pCloneNode = pNode->m_pNext;
pNode->m_pNext = pCloneNode->m_pNext;
pNode = pNode->m_pNext;
}
while(pNode != NULL)
{
pCloneNode->m_pNext = pNode->m_pNext;
pCloneNode = pCloneNode->m_pNext;
pNode->m_pNext = pCloneNode->m_pNext;
pNode = pNode->m_pNext;
}
return pCloneHead;
}
ComplexListNode* Clone(ComplexListNode* pHead)
{
CloneNodes(pHead);
ConnectsiblingNodes(pHead);
return ReconnectNodes(pHead);
}