leetcode 284. Peeking Iterator

class PeekingIterator(object):
    def __init__(self, iterator):
        """
        Initialize your data structure here.
        :type iterator: Iterator
        """
        self.iter = iterator
        self.buffer = self.iter.next() if self.iter.hasNext() else None

    def peek(self):
        """
        Returns the next element in the iteration without advancing the iterator.
        :rtype: int
        """
        return self.buffer

    def next(self):
        """
        :rtype: int
        """
        res = self.buffer
        self.buffer = self.iter.next() if self.iter.hasNext() else None
        return res
        
    def hasNext(self):
        """
        :rtype: bool
        """
        return self.buffer is not None

;

Popular Programs