My Python With Stack


  • 0
    J
    class PeekingIterator(object):
        def __init__(self, iterator):
            """
            Initialize your data structure here.
            :type iterator: Iterator
            """
            self.l = []
            while iterator.hasNext():
                self.l.append(iterator.next())
            self.l = self.l[::-1]
    
        def peek(self):
            """
            Returns the next element in the iteration without advancing the iterator.
            :rtype: int
            """
            if len(self.l) > 0:
                return self.l[-1]
            else:
                return
    
        def next(self):
            """
            :rtype: int
            """
            return self.l.pop()
    
        def hasNext(self):
            """
            :rtype: bool
            """
            if len(self.l) > 0:
                return True
            else:
                return False
    

Log in to reply
 

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