My ac solution python

  • 0
    class Solution:
        # @param head, a ListNode
        # @return a ListNode
        def sortList(self, head):
            #constant space
            if head is None:
            copy = []
            pr = head
            while pr is not None:
                copy.append((pr.val, pr))
                pr =
            # copy contains val and address of one node
            copy = sorted(copy)#O(nlgn) time can use quick sort
            for i in range(len(copy)-1):
                _, adr = copy[i]
       = copy[i+1][1]
            copy[-1][1].next = None
            head = copy[0][1]
            return head

  • 0

    This is easy but I don't think this is constant space.

  • 0

    yes you're right it's O(N) space

Log in to reply

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