Python O(1) space


  • 0
    E
        def insertionSortList(self, head):
            if not head: return
            buffr = ListNode(None)
            buffr.next, jP, j = head, head, head.next
            while j:
                if j.val > jP.val: # already in place
                    jP, j = j, j.next
                else:
                    i = buffr
                    while i.next != j and i.next.val < j.val: # look for insertion spot
                        i = i.next
                    i.next, j.next, jP.next, j = j, i.next, j.next, j.next
            return buffr.next
    

Log in to reply
 

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