合并2个递增链表,使得合并后仍保持递增顺序:
MergeList.cpp:
/*合并2个排序的链表,2个递增的排序链表,合并这2个使得新链表中的结点仍是按照递增顺序排列的*/
#include <iostream>
#include <stdio.h>
#include "List.h"
using namespace std;
ListNode* MergeList(ListNode* pHead1,ListNode* pHead2)
{
if(pHead1 == NULL)
return pHead2;
if(pHead2 == NULL)
return pHead1;
ListNode* pMerge = NULL;
if(pHead1 ->m_value <= pHead2 -> m_value)
{
pMerge = pHead1;
pMerge -> m_pNext = MergeList(pHead1->m_pNext ,pHead2);
}
else
{
pMerge = pHead2;
pHead2 -> m_pNext = MergeList(pHead1,pHead2->m_pNext);
}
return pMerge;
}
// ====================测试代码====================
ListNode* Test(char* testName, ListNode* pHead1, ListNode* pHead2)
{
if(testName != NULL)
cout<<"%s begins:"<< testName<<endl;
cout<<"The first list is:";
PrintList(pHead1);
cout<<"\n"<<"The second list is:";
PrintList(pHead2);
cout<<"\n"<<"The merged list is:";
ListNode* pMergedHead = MergeList(pHead1, pHead2);
PrintList(pMer