Python: totally in place, no extra memory requirement


  • 0
    J

    '''
    class Solution(object):
    def mergeTwoLists(self, l1, l2):
    """
    :type l1: ListNode
    :type l2: ListNode
    :rtype: ListNode
    """
    if not l1 or not l2:
    return l1 or l2
    else:
    # Head Initialization
    if l1.val > l2.val:
    dummyHead = l2
    hold = l1
    go = l2
    else:
    dummyHead = l1
    hold = l2
    go = l1
    previousGo = None

            while go.next:
                while go.val <= hold.val:
                    previousGo = go
                    if go.next:
                        go = go.next
                    else:
                        break
                if not previousGo.next:
                    break
                previousGo.next = hold
                go, hold = hold, go
            go.next = hold
            
            return dummyHead       
    

    '''


Log in to reply
 

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