Coincise python solution


  • 0
    M

    class NestedIterator(object):

    def __init__(self, nestedList):
        """
        Initialize your data structure here.
        :type nestedList: List[NestedInteger]
        """
        self.mylist = nestedList
        
    
    def next(self):
        """
        :rtype: int
        """
        self.hasNext()
        return self.mylist.pop(0).getInteger()
    
    def hasNext(self):
        """
        :rtype: bool
        """
        while self.mylist:
            if self.mylist[0].isInteger():
                return True;
            else :
                lst = self.mylist[0].getList()
                if not lst:
                    self.mylist.pop(0)
                else :
                    self.mylist = lst+self.mylist[1:]
                
        return False

Log in to reply
 

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