1 #include "stdafx.h" 2 #include <iostream> 3 /* 4 题目:合并两个排序的链表 5 输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的。 6 链表结点定义如下: 7 struct ListNode 8 { 9 int m_nValue; 10 ListNode *m_pNext; 11 } 12 */ 13 using namespace std; 14 struct ListNode 15 { 16 int m_nValue; 17 ListNode *m_pNext; 18 }; 19 ListNode* Merge(ListNode* pHead1,ListNode* pHead2) 20 { 21 if(pHead1 == NULL) 22 return pHead2; 23 else if(pHead2 == NULL) 24 return pHead1; 25 ListNode* pMergedHead = NULL; 26 if(pHead1->m_nValue<pHead2->m_nValue) 27 { 28 pMergedHead = pHead1; 29 pMergedHead->m_pNext=Merge(pHead1->m_pNext,pHead2); 30 } 31 else 32 { 33 pMergedHead = pHead2; 34 pMergedHead->m_pNext=Merge(pHead1,pHead2->m_pNext); 35 } 36 return pMergedHead; 37 } 38 int _tmain(int argc, _TCHAR* argv[]) 39 { 40 41 return 0 ; 42 }