合并两个排序的链表

时间:2021-01-12 04:16:44

    题目:输入两个递增的排序的链表,合并这两个链表并使新链表中的节点仍然是按照递增顺序的。链表的结构如下:

     struct ListNode{

    int m_nValue;

    ListNode* m_pNext;

  }

 1 struct ListNode{
2 int m_nValue;
3 ListNode* m_pNext;
4 }
5
6 ListNode* Merge(ListNode* pHead1,ListNode* pHead2){
7 if(pHead1 == NULL)
8 return pHead2;
9 else if(pHead2 == NULL)
10 return pHead1;
11
12 ListNode* pHead3 = NULL;
13 if(pHead1->m_nValue < pHead2->m_nValue){
14 pHead3 = pHead1;
15 pHead3->m_pNext = Merge(pHead1->m_pNext,pHead2);
16 }else{
17 pHead3 = pHead2;
18 pHead3->m_pNext = Merge(pHead1,pHead2->m_pNext);
19 }
20 return pHead3;
21 }