美文网首页LeetCode蹂躏集
LeetCode 21. Merge Two Sorted Li

LeetCode 21. Merge Two Sorted Li

作者: alexsssu | 来源:发表于2018-01-29 13:04 被阅读0次

    将两个有序链表合称为一个有序链表。

    class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            ListNode l3(INT_MIN);
            ListNode* tail = &l3;
            while (l1 && l2) {
                if (l1->val < l2->val) {
                    tail->next = l1;
                    l1 = l1->next;
                }else{
                    tail->next = l2;
                    l2 = l2->next;
                }
                tail = tail->next;
            }
            tail->next = l1 ? l1 : l2;
            return l3.next;
        }
    };
    

    相关文章

      网友评论

        本文标题:LeetCode 21. Merge Two Sorted Li

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