import java.util.Iterator;

/**
 * Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that
 * support the peek() operation -- it essentially peek() at the element that will be returned by the next call to
 * next().
 *
 * Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3].
 *
 * Call next() gets you 1, the first element in the list.
 *
 * Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.
 *
 * You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false.
 *
 * Hint:
 *
 * Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not?
 *
 * Test your design with call order of peek() before next() vs next() before peek().
 *
 * For a clean implementation, check out Google's guava library source code.
 *
 * Follow up: How would you extend your design to be generic and work with all types, not just integer?
 *
 * Tags: Design
 *
 * Similar Problems: (M) Binary Search Tree Iterator, (M) Flatten 2D Vector, (M) Zigzag Iterator
 */
public class PeekingIterator implements Iterator<Integer> {

    private Integer next;
    private Iterator<Integer> iter;

    /**
     * Store result in next
     * @param iterator
     */
    public PeekingIterator(Iterator<Integer> iterator) {
        // initialize any member here.
        this.iter = iterator;
        if (iterator.hasNext()) {
            next = iterator.next();
        }
    }

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

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        int res = next;
        next = iter.hasNext() ? iter.next() : null;
        return res;
    }

    @Override
    public boolean hasNext() {
        return next != null;
    }

}

@@ -1,8 +1,60 @@
+package com.freetymekiyan.algorithms.level.medium;%0A%0A
 import j
@@ -317,24 +317,28 @@
 * next().%0A *
+ %3Cp%3E
 %0A * Here is 
@@ -428,24 +428,28 @@
 1, 2, 3%5D.%0A *
+ %3Cp%3E
 %0A * Call nex
@@ -493,24 +493,28 @@
 the list.%0A *
+ %3Cp%3E
 %0A * Now you 
@@ -601,24 +601,28 @@
 return 2.%0A *
+ %3Cp%3E
 %0A * You call
@@ -728,24 +728,28 @@
 rn false.%0A *
+ %3Cp%3E
 %0A * Hint:%0A *
@@ -748,16 +748,20 @@
 Hint:%0A *
+ %3Cp%3E
 %0A * Thin
@@ -817,16 +817,19 @@
 element.
+%0A *
  Is one 
@@ -865,19 +865,16 @@
 hy not?%0A
- *%0A
  * Test 
@@ -950,19 +950,16 @@
 peek().%0A
- *%0A
  * For a
@@ -1029,16 +1029,20 @@
 code.%0A *
+ %3Cp%3E
 %0A * Foll
@@ -1140,16 +1140,58 @@
 eger?%0A *
+ %3Cp%3E%0A * Company Tags: Google, Apple, Yahoo
 %0A * Tags
@@ -1203,11 +1203,8 @@
 ign%0A
- *%0A
  * S
@@ -1443,51 +1443,131 @@
   * 
-Store result in next%0A     * @param iterator
+Peeking iterator is based on normal iterator.%0A     * Just a wrapper class.%0A     * Use a variable to cache the next element.
 %0A   
@@ -1780,24 +1780,79 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * Just return the cached element.%0A     */%0A
     // Retur
@@ -1972,24 +1972,191 @@
 ext;%0A    %7D%0A%0A
+    /**%0A     * Return next value.%0A     * If iterator still has more elements, update next with next.%0A     * If iterator don't have any more, set next to null.%0A     */%0A
     // hasNe
@@ -2397,24 +2397,64 @@
 res;%0A    %7D%0A%0A
+    /**%0A     * Just check next.%0A     */%0A
     @Overrid

update Peeking Iterator