# 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

## 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]

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

## Peeking Iterator LeetCode SolutionC++

``````class PeekingIterator : public Iterator {
private:
int m_next;
bool m_hasnext;
public:
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 SolutionJava

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

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

public Integer peek() {
return next;
}

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

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

## Peeking Iterator SolutionJavaScript

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

PeekingIterator.prototype.peek = function() {
if(this.peeked!==null){
return this.peeked;
}
if(this.iterator.hasNext()!==false){
this.peeked = this.iterator.next();
}
return this.peeked;
};

PeekingIterator.prototype.next = function() {
var val = "";
if(this.peeked!==null){
val = this.peeked;
this.peeked = null;
return val;
}
return this.iterator.next();
};

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

## Peeking Iterator SolutionPython

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

def peek(self):
return self.temp

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

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