Python version of the top vote solution

  • 0

    To avoid modifying the input nestedList, self.stack = nestedList should be self.stack = nestedList[:]

    def __init__(self, nestedList):
        self.stack = nestedList
    def next(self):
        return self.stack.pop(0).getInteger()
    def hasNext(self):
        while self.stack:
            cur = self.stack[0]
            if cur.isInteger():
                return True
            self.stack = cur.getList + self.stack
        return False

  • 0
    This post is deleted!

Log in to reply

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