Why My Python code will cause "Time Limit Exceeded"?

  • 0

    Here is my code, and it perform well in Sublime. I dont know why it will cause "Time Limit Exceeded" in Leetcode. Can anyone help me to figure out the problem? Thanks a lot!

    def reverseList(self, head):
    	if not head:
    		return None
    	pre = head
    	curr = head.next
    	while curr:
    		temp = curr.next
    		curr.next = pre
    		pre = curr
    		curr = temp
    	return pre

  • 1

    That code doesn't get "Time Limit Exceeded". It gets "IndentationError".

    But even after fixing that, it's clearly wrong, as you build an infinite list (because you never set head.next to None, so the last element of the reversed list points to the second-to-last).

    So it certainly doesn't "perform well in Sublime", as it produces a wrong result. Don't you check your results?

  • 0

    Thanks for ur response. There is an IndentationError since i dont format my code well in this discussion. So that isn't the real problem. However, I really miss the infinite case. Thanks a lot! :-)

Log in to reply

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