合并两个排序的链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
  if(pHead1 == NULL)
    return pHead2;
  else if(pHead2 == NULL)
    return pHead1;

  ListNode* newhead = NULL;
  if(pHead1->val < pHead2->val)
  {
    newhead = pHead1;
    newhead->next = Merge(pHead1->next,pHead2);
  }
  else
  {
    newhead = pHead2;
    newhead->next = Merge(pHead1,pHead2->next);
  }
    return newhead;
}
};

怎么能这么强的

posted @ 2020-05-23 22:28  转瞬即逝1995  阅读(75)  评论(0编辑  收藏  举报