Python solution, constant space


  • 0
    T
        def __init__(self, iterator):
            self.i = iterator
            self.n = None
    
        def peek(self):
            if not self.n:
                self.n = self.i.next()
            return self.n
    
        def next(self):
            if not self.n:
                self.n = self.i.next()
            a = self.n
            self.n = None
            return a
    
        def hasNext(self):
            if self.n:
                return True
            return self.i.hasNext()

Log in to reply
 

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