A C++ solution


  • 0
    W
    class Solution 
    {
    public:
        
        ListNode* mergeTwoLists(ListNode* a, ListNode* b) 
        {
            if(!a||!b)
                return a?a:b;
            
            if(a->val > b->val)
                swap(a, b);
            auto head=a;
            while(a && b)
            {
                if(!a->next || a->next->val > b->val)
                    swap(a->next, b);
                a=a->next;
            }
            return head;
        }
    };
    

Log in to reply
 

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