美文网首页
merge-two-sorted-lists

merge-two-sorted-lists

作者: DaiMorph | 来源:发表于2019-06-24 23:57 被阅读0次
    class Solution {
    public:
        ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
            if(!l1)return l2;
            if(!l2)return l1;
            ListNode*head=new ListNode(-1);
            ListNode*p=head;
            while(l1&&l2)
            {
                if(l1->val<l2->val)p->next=l1,p=p->next,l1=l1->next;
                else p->next=l2,p=p->next,l2=l2->next;
            }
            if(l1)p->next=l1;
            if(l2)p->next=l2;
            return head->next;
        }
    };
    

    相关文章

      网友评论

          本文标题:merge-two-sorted-lists

          本文链接:https://www.haomeiwen.com/subject/ywfzqctx.html