Why I have Time Limit Exceed error ?


  • 0
    A

    My following solution give me the Time Limit Exceed error. But when I print the while statement, it only executes twice. So how is it possible to have this error? Anyone can help?? Thanks!

    class Solution(object):
        def partition(self, head, x):
            """
            :type head: ListNode
            :type x: int
            :rtype: ListNode
            """
            l1 = ListNode(0)
            l1_cur = l1
            l2 = ListNode(0)
            l2_cur = l2
            while head:
                print '--within while--'
                if head.val < x:
                    l1_cur.next = head
                    l1_cur = l1_cur.next
                else:
                    l2_cur.next = head
                    l2_cur = l2_cur.next
                head = head.next
            l1_cur.next = l2.next
            return l1.next

  • 1
    Y

    You forgot to set the end of the list to null, and it ends up a circular list.


  • 0
    A

    Thanks! I will try that


Log in to reply
 

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