1.输入两个递增排序的链表,合并两个链表并使新链表中的结点仍然是递增排序的。
![](https://img-blog.csdn.net/20160705184556990?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
结果:
![](https://img-blog.csdn.net/20160705184644226?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
结果图示:
分析:从合并两个 链表的头节点开始,链表1的头节点小于链表2的头节点的值,因此链表1的头节点是合并之后的头节点。然后在剩余的结点中,链表2的头节点小于链表1的头节点,因此链表2的头节点是剩余结点的头节点,发现合并剩余结点的步骤和之前的过程是一样的,可以使用递归的方法来合并。
图示:
源码:
/*合并两个已经排序的链表*/
#include<iostream>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
//创建一个节点
ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->m_nValue = value;
pNode->m_pNext = NULL;
return pNode;
}
//打印整个链表
void PrintList(ListNode* pHead)
{
printf("PrintList starts.\n");
ListNode* pNode = pHead;
while (pNode != NULL)
{
printf("%d\t", pNode->m_nValue);
pNode = pNode->m_pNext;
}
printf("\nPrintList ends.\n");
}
//打印结点的值
void PrintListNode(ListNode* pNode)
{
if (pNode == NULL)
{
printf("The node is NULL\n");
}
else
{
printf("The key in node is %d.\n", pNode->m_nValue);
}
}
//将两个结点连接起来
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if (pCurrent == NULL)
{
printf("Error to connect two nodes.\n");
exit(1);
}
pCurrent->m_pNext = pNext;
}
//销毁链表
void DestroyList(ListNode* pHead)
{
ListNode* pNode = pHead;
while (pNode != NULL)
{
pHead = pHead->m_pNext;
delete pNode;
pNode = pHead;
}
}
//合并链表--递归的方法
ListNode* MergeRecursive(ListNode* pHead1, ListNode* pHead2)
{
if (pHead1 == NULL)
{
return pHead2;
}
else if (pHead2 == NULL)
{
return pHead1;
}
ListNode* resultHead = NULL;//合并之后的头节点
if (pHead1->m_nValue < pHead2->m_nValue)
{
resultHead = pHead1;
resultHead->m_pNext = MergeRecursive(pHead1->m_pNext, pHead2);//递归合并
}
else
{
resultHead = pHead2;
resultHead->m_pNext = MergeRecursive(pHead1, pHead2->m_pNext);//递归合并
}
return resultHead;
}
//非递归的方法
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if (pHead1 == NULL)
{
return pHead2;
}
else if (pHead2 == NULL)
{
return pHead1;
}
ListNode* resultHead = NULL;
if (pHead1->m_nValue < pHead2->m_nValue)//确定头节点
{
resultHead = pHead1;
pHead1 = pHead1->m_pNext;
}
else
{
resultHead = pHead2;
pHead2 = pHead2->m_pNext;
}
ListNode* pNode = resultHead;//将头节点保存下来
while (pHead1 != NULL&&pHead2 != NULL)
{
if (pHead1->m_nValue < pHead2->m_nValue)
{
pNode->m_pNext = pHead1;
pNode = pHead1;//更新pNode的位置,向前
pHead1 = pHead1->m_pNext;
}
else
{
pNode->m_pNext = pHead2;
pNode = pHead2;
pHead2 = pHead2->m_pNext;
}
}
if (pHead1 == NULL)//当链表1达到末尾
{
pNode = pHead2;
}
else if (pHead2==NULL)//链表2达到末尾
{
pNode = pHead1;
}
return resultHead;
}
int main()
{
ListNode* pNode1 = CreateListNode(1);
ListNode* pNode2 = CreateListNode(3);
ListNode* pNode3 = CreateListNode(5);
ListNode* pNode4 = CreateListNode(7);
ListNode* pNode5 = CreateListNode(9);
ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);
printf("the first list: \n");
PrintList(pNode1);
ListNode* pNode11 = CreateListNode(2);
ListNode* pNode12 = CreateListNode(4);
ListNode* pNode13 = CreateListNode(6);
ListNode* pNode14 = CreateListNode(8);
ConnectListNodes(pNode11, pNode12);
ConnectListNodes(pNode12, pNode13);
ConnectListNodes(pNode13, pNode14);
printf("\nthe second list: \n");
PrintList(pNode11);
printf("after merging:\n");
ListNode* resultHead = Merge(pNode1, pNode11);
PrintList(resultHead);
DestroyList(resultHead);
system("PAUSE");
return 0;
}
结果: