Time Limit Exceeded(python),why?


  • 0
    D
    class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def sortList(self, head):
        if head is None or head.next is None:
            return head
        mid = (head.val + head.next.val) / 2
        if head.val > head.next.val:
            lhead, rhead = head.next, head
        else:
            lhead, rhead = head, head.next
        lit, rit = lhead, rhead
        it = head.next.next
        while it is not None:
            if it.val >= mid:
                rit.next = it
                rit = it
            else:
                lit.next = it
                lit = it
            it = it.next
        lit.next, rit.next = None, None
        lhead = self.sortList(lhead)
        rhead = self.sortList(rhead)
        it = lhead
        while it.next is not None:
            it = it.next
        it.next = rhead
        return lhead

Log in to reply
 

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