Avoid recursion in Python!

  • 0

    I was getting a mysterious error at an input length of 20000 for my Python solution. It turns out to be because I'd implemented linked list length counting recursively and I was hitting a stack depth limit. It would be great if the error message were more clear, because this is a totally valid thing to write for an interview:

    def length(node):
       return 0 if not node else 1 + length(node.next)


Log in to reply

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