Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Peeking Iterator problem solution

YASH PAL, 31 July 2024

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

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

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

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes