Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Peeking Iterator problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Peeking Iterator problem solution Design, an iterator that supports the peek operation on a list in addition to the hasNext and the next operations.

Implement the PeekingIterator class:

  1. PeekingIterator(int[] nums) Initializes the object with the given integer array nums.
  2. int next() Returns the next element in the array and moves the pointer to the next element.
  3. bool hasNext() Returns true if there are still elements in the array.
  4. int peek() Returns the next element in the array without moving the pointer.
Leetcode Peeking Iterator problem solution

Leetcode Peeking Iterator problem solution in Python.

import copy

class PeekingIterator(object):
    def __init__(self, iterator):
        self.iter = iterator

    def peek(self):
        peekIter = copy.copy(self.iter)
        return next(peekIter)

    def next(self):
        return next(self.iter)

    def hasNext(self):
        peekIter = copy.copy(self.iter)
        return next(peekIter, None) is not None

Peeking Iterator problem solution in Java.

class PeekingIterator implements Iterator<Integer> {

    Iterator<Integer> iterator;
    Integer peek = Integer.MIN_VALUE;
    public PeekingIterator(Iterator<Integer> iterator) {
        // initialize any member here.
        this.iterator = iterator;
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        if(peek == Integer.MIN_VALUE){
            peek = iterator.next();
            return peek; 
        }else{
            return peek;
        }
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        if(peek == Integer.MIN_VALUE){
            return iterator.next();
        }else{
            int value = peek;
            peek = Integer.MIN_VALUE;
            return value;   
        }
    }

    @Override
    public boolean hasNext() {
        return iterator.hasNext() || peek != Integer.MIN_VALUE;
    }
}

Problem solution in C++.

class PeekingIterator : public Iterator {
public:
    int val;
    int *cache=0;
    PeekingIterator(const vector<int>& nums) : Iterator(nums) {

    }

    int peek() {
        if (!cache) {
            val=Iterator::next();
            cache=&val;
        }
        return *cache;
    }

    int next() {
        if (cache) {
            int ret=*cache;
            cache=0;
            return ret;
        }
        else return Iterator::next();
    }

    bool hasNext() const {
        if (!Iterator::hasNext()&&!cache) return false;
        else return true;
    }
};

Problem solution in C.

struct PeekingIterator {
    struct Iterator* iterator;
    bool hasPeeked;
    int next;
};

struct PeekingIterator* Constructor(struct Iterator* iter) {
    struct PeekingIterator* piter = malloc(sizeof(struct PeekingIterator));
    piter->iterator = iter;
    piter->hasPeeked = false;
    return piter;
}

int peek(struct PeekingIterator* obj) {
    if (!obj->hasPeeked) {
        obj->next = obj->iterator->next();
        obj->hasPeeked = true;
    }
    return obj->next;
}

int next(struct PeekingIterator* obj) {
    if (obj->hasPeeked) {
        obj->hasPeeked = false;
        return obj->next;
    }
    return obj->iterator->next();
}

bool hasNext(struct PeekingIterator* obj) {
    if (obj->hasPeeked) {
        return true;
    }
    return obj->iterator->hasNext();
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes