Clean concise mergesort Python solution

  • 1
    class Solution(object):
        def sortList(self, head):
            :type head: ListNode
            :rtype: ListNode
            if not head:
                return None
            if not
                return head
            pre, slow, fast = None, head, head
            while fast and
                pre, slow, fast = slow,,
   = None
            left, right = self.sortList(head), self.sortList(slow)
            pre_head = cur = ListNode(None)
            while left and right:
                if left.val < right.val:
          ,, left = left, None,
          ,, right = right, None,
                cur =
            if left or right:
       = left or right

Log in to reply

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