Python version of the top vote solution


  • 0
    D

    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.pop(0)
            self.stack = cur.getList + self.stack
    
        return False

  • 0
    D
    This post is deleted!

Log in to reply
 

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