Python recursive solution


  • 0
    Z
    def __init__(self, nestedList):
        """
        Initialize your data structure here.
        :type nestedList: List[NestedInteger]
        """
        self.stack = nestedList[::-1]
    
    def next(self):
        """
        :rtype: int
        """
        if self.hasNext():
            return self.stack.pop().getInteger()
        
    
    def hasNext(self):
        """
        :rtype: bool
        """
        while self.stack:
            curr = self.stack.pop()
            if curr.isInteger():
                self.stack.append(curr)
                return True
            self.stack += curr.getList()[::-1]
        return False

Log in to reply
 

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