Python 56ms solution


  • 1
    A
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            if l1 is None:
                return l2
            if l2 is None:
                return l1
           
            f = l1
            s = l2
            newHead = new = ListNode('dummy')
            while f and s:
                if f.val <= s.val:
                    new.next = f
                    f = f.next
                else:
                    new.next = s
                    s = s.next
                new = new.next
                    
    
            new.next = f if f else s
            return newHead.next

Log in to reply
 

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