Python solution, constant space

  • 0
        def __init__(self, iterator):
            self.i = iterator
            self.n = None
        def peek(self):
            if not self.n:
                self.n =
            return self.n
        def next(self):
            if not self.n:
                self.n =
            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.