Peeking Iterator LeetCode Solution

Here, We see Peeking Iterator LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Peeking Iterator LeetCode Solution

Peeking Iterator LeetCode Solution

Problem Statement

Design an iterator that supports the peek operation on an existing iterator in addition to the hasNext and the next operations.

Implement the PeekingIterator class:

  • PeekingIterator(Iterator<int> nums) Initializes the object with the given integer iterator iterator.
  • int next() Returns the next element in the array and moves the pointer to the next element.
  • boolean hasNext() Returns true if there are still elements in the array.
  • int peek() Returns the next element in the array without moving the pointer.

Note: Each language may have a different implementation of the constructor and Iterator, but they all support the int next() and boolean hasNext() functions.

Example 1:Input [“PeekingIterator”, “next”, “peek”, “next”, “next”, “hasNext”] [[[1, 2, 3]], [], [], [], [], []]
Output [null, 1, 2, 2, 3, false]

PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]; // return 1, the pointer moves to the next element [1,2,3].
peekingIterator.peek(); // return 2, the pointer does not move [1,2,3].; // return 2, the pointer moves to the next element [1,2,3]; // return 3, the pointer moves to the next element [1,2,3]
peekingIterator.hasNext(); // return False

Peeking Iterator LeetCode Solution C++

class PeekingIterator : public Iterator {
    int m_next;
    bool m_hasnext;
	PeekingIterator(const vector<int>& nums) : Iterator(nums) {
	    m_hasnext = Iterator::hasNext();
	    if (m_hasnext) m_next = Iterator::next();

	int peek() {
        return m_next;

	int next() {
	    int t = m_next;
	    m_hasnext = Iterator::hasNext();
	    if (m_hasnext) m_next = Iterator::next();
	    return t;

	bool hasNext() const {
	    return m_hasnext;
};Code language: HTML, XML (xml)

Peeking Iterator LeetCode Solution Java

class PeekingIterator implements Iterator<Integer> {  
    private Integer next = null;
    private Iterator<Integer> iter;

    public PeekingIterator(Iterator<Integer> iterator) {
        iter = iterator;
        if (iter.hasNext())
            next =;
    public Integer peek() {
        return next; 

    public Integer next() {
        Integer res = next;
        next = iter.hasNext() ? : null;
        return res; 

    public boolean hasNext() {
        return next != null;
}Code language: PHP (php)

Peeking Iterator Solution JavaScript

var PeekingIterator = function(iterator) {
    this.peeked = null;
    this.iterator = iterator;

PeekingIterator.prototype.peek = function() {
        return this.peeked;
        this.peeked =;
    return this.peeked; 
}; = function() {
    var val = "";
        val = this.peeked;
        this.peeked = null;
        return val;

PeekingIterator.prototype.hasNext = function() {
        return true;
    return this.iterator.hasNext();
};Code language: JavaScript (javascript)

Peeking Iterator Solution Python

class PeekingIterator(object):
    def __init__(self, iterator):
        self.iter = iterator
        self.temp = if self.iter.hasNext() else None

    def peek(self):
        return self.temp

    def next(self):
        ret = self.temp
        self.temp = if self.iter.hasNext() else None
        return ret

    def hasNext(self):
        return self.temp is not None
Scroll to Top