C++ 6ms Recursive clean solution


  • 1
    P
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            ListNode* result = NULL;
            
            if(l1 == NULL) return l2;
            if(l2 == NULL) return l1;
            
            if(l1->val <= l2->val){
                result = l1;
                result->next = mergeTwoLists(l1->next, l2);
            }else{
                result = l2;
                result->next = mergeTwoLists(l1, l2->next);
            }
    return result;
    }
    
        }

Log in to reply
 

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