码迷,mamicode.com
首页 > 其他好文 > 详细

复杂链表的复制

时间:2015-07-09 16:18:28      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

解法一:

技术分享

解法二:

技术分享

技术分享

void CloneNodes(ComplexListNode* pHead)
{
ComplexListNode* pNode = pHead;
while (pNode != NULL)
{
ComplexListNode* pCLoned = new ComplexListNode();
pCloned->m_nValue = pNode->m_nValue;
pCloned->m_pNext = pNode->m_pNext;
pNode->m_pNext = pCloned;
pNode = pCloned->m_pNext;
}
}

技术分享

void ConnectSiblingNodes(ComplexListNode* pHead)
{
ComplexListNode* pNode = pHead;
while (pNode != NULL)
{
ComplexListNode*pCloned = pNode->m_pNext;
if (pNode->m_pSibling != NULL)
{
pCloned->m_pSibling = pNode->m_pSibling->m_pNext;


}
pNode = pCloned->m_pNext;
}
}

技术分享

ComplexListNode* ReconnectNodes(ComplexListNode* pHead)
{
ComplexListNode* pNode = pHead;
ComplexListNode* pClonedHead = NULL;
ComplexListNode* pClonedNode = NULL;
if (pNode != NULL)
{
pClonedHead = pClonedNode = pNode->m_pNext;
pNode->m_pNext = pClonedNode->m_pNext;
}
while (pNode != NULL)
{
pClonedNode->m_pNext = pNode->m_pNext;
pClonedNode = pClonedNode->m_pNext;
pNode->m_pNext = pClonedNode->m_pNext;
pNode = pNode->m_pNext;
}
return pClonedHead;
}

把上面三步合起来,就是复制链表的完整过程:

ComplexListNode* Clone(ComplexListNode* pHead)

{

       CloneNodes(pHead);

       ConnectSiblingNodes(pHead);

       return ReconnectNodes(pHead);

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

复杂链表的复制

标签:

原文地址:http://blog.csdn.net/wangfengfan1/article/details/46814333

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!