52 ms Python solution


  • 1
    J

    class Solution(object):
    def mergeTwoLists(self, l1, l2):

        # create dummy head to start out with
        p = dummy = ListNode(None)
        
        while l1 != None and l2 != None:
            if l1.val < l2.val:
                p.next = l1
                l1 = l1.next
            else:
                p.next = l2
                l2 = l2.next
            p = p.next
            
        if l1 != None:  p.next = l1
        if l2 != None:  p.next = l2
        
        return dummy.next

Log in to reply
 

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