My Ruby Solution


  • 0
    T
    def merge_two_lists(l1, l2)
        return l1 || l2 unless l1 && l2
        
        l1, l2 = l2, l1 unless l1.val < l2.val
        l3, l1 = l1, l1.next
        
        p3 = l3
        
        while l1 && l2
            if l1.val < l2.val
                p3.next = l1
                l1 = l1.next
            else
                p3.next = l2
                l2 = l2.next
            end
            
            p3 = p3.next
        end
        p3.next = l1 || l2
        
        l3
    end
    

Log in to reply
 

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