题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的。
链表结点定义如下:
struct ListNode { int m_nValue; ListNode * m_pNext; }
代码如下:
ListNode * Merge(ListNode * head1, ListNode *head2) { if(head1==NULL) { return head2; } if(head2==NULL) { return head1; } ListNode * pMergeHead = NULL; if(head1->m_nValue > head2->m_nValue) { pMergeHead = pHead1; pMergeHead->m_pNext = Merge(head1,head2->m_pNext); } else { pMergeHead = pHead2; pMergeHead->m_pNext = Merge(head1->m_pNext,head2); } return pMergeHead; }