标签:合并链表
题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的。链表的定义如下:
struct ListNode { int m_nValue; ListNode* m_pNext; };
分析:本题考查的是大量指针的编程问题和程序鲁棒性,比如输入空指针情况。解法如下:
ListNode* Merge(ListNode* pHead1,ListNode* pHead2) { if(pHead1==NULL) return pHead2; if(pHead2==NULL) return pHead1; ListNode* pMergedHead=NULL; if(pHead1->m_nValue<pHead2->m_nValue) { pMergedHead=pHead1; pMergedHead->m_pNext=Merge(pHead1->m_pNext,pHead2); } else { pMergedHead=pHead2; pMergedHead->m_pNext=Merge(pHead1,pHead2->m_pNext); } return pMergedHead; }
本文出自 “仙路千叠惊尘梦” 博客,请务必保留此出处http://secondscript.blog.51cto.com/9370042/1583459
标签:合并链表
原文地址:http://secondscript.blog.51cto.com/9370042/1583459