Python non-recursive solution


  • -1
    R
    class Solution:
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}
    def mergeTwoLists(self, l1, l2):
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        small, large = (l1, l2) if l1.val < l2.val else (l2, l1)
        head = small
        while small.next != None:
            if small.next.val > large.val:
                small.next, large = large, small.next
            small = small.next
        small.next = large
        return head

Log in to reply
 

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