Python solution in constant space


  • 0
    A
    def __init__(self, iterator):
        self.itera = iterator
        self.nextBool = False
        if self.itera.hasNext():
            self.nextBool = True
            self.now = self.itera.next()
        
    def peek(self):
        return self.now
    
    def next(self):
        tmp = self.now
        if self.itera.hasNext():
            self.nextBool = True
            self.now = self.itera.next()
        else:
            self.nextBool = False
        return tmp
    
    def hasNext(self):
        return self.nextBool

Log in to reply
 

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