c++ solution implemented with 1 addition node


  • 0
    T
    class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            ListNode Head(0), *ptr = &Head;
            while (l1 && l2) {
                ptr->next = l1->val < l2->val ? l1:l2;
                ptr->next == l1 ? l1=l1->next:l2 = l2->next, ptr = ptr->next;
            }
            ptr->next = l1 ? l1:l2;
            return Head.next;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.