合并两个排序的链表(递归算法)

时间:2021-01-12 04:11:50
输入两个递增排序的链表,合并这两个链表并使新链表中的结构任然递增!
 1 ///////////////////合并两个排序的链表(递归算法)/////////////////////////////////
 2 ListNode* Merge(ListNode* pHead1 ,ListNode* pHead2)
 3 {
 4     if (pHead1==NULL)
 5     {
 6         return pHead2;
 7     }
 8     if (pHead2 == NULL)
 9     {
10         return pHead1;
11     }
12     ListNode* pMergedHead = NULL;
13     if (pHead1->m_nValue <= pHead2->m_nValue)
14     {
15         pMergedHead = pHead1 ;
16         pMergedHead->m_pNext = Merge(pHead1->m_pNext,pHead2);
17     }
18     else
19     {
20         pMergedHead = pHead2 ;
21         pMergedHead->m_pNext = Merge(pHead1,pHead2->m_pNext);
22     }
23     return pMergedHead ;
24 
25 }