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
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?