publicPeekingIterator(Iterator<Integer> iterator) { // initialize any member here. this.iterator = iterator; if (this.iterator.hasNext()) { top = this.iterator.next(); } }
// Returns the next element in the iteration without advancing the iterator. public Integer peek() { return top; }
// hasNext() and next() should behave the same as in the Iterator interface. // Override them if needed. @Override public Integer next() { Integerans= top; if (this.iterator.hasNext()) { top = this.iterator.next(); } else { top = null; } return ans; }
@Override publicbooleanhasNext() { return top != null; } }