Simple Solution in C++


  • 0
    M
    class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            ListNode * result = 0;
            ListNode ** result_ref = &result;
            while (l1 && l2) {
                if (l1->val < l2->val) {
                    *result_ref = l1;
                    l1 = l1->next;
                } else {
                    *result_ref = l2;
                    l2 = l2->next;
                }
                result_ref = &(*result_ref)->next;
            }
            if (l1) *result_ref = l1;
            else if (l2) *result_ref = l2;
            return result;
        }
    };
    

Log in to reply
 

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