concise c++ code


  • 0
    J

    I think the code is already self-explained

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            ListNode *dummy = new ListNode(-1), *cur = dummy;
            while(l1 && l2){
                bool flag = l1->val < l2->val;
                cur = cur->next = flag? l1 : l2;
                l1 = flag ? l1->next : l1;
                l2 = flag ? l2 : l2->next; 
            }
            cur->next = l1 ? l1 : l2;
            return dummy->next;
        }
    

Log in to reply
 

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