Easiest Java Solution :)


  • 0
    class PeekingIterator implements Iterator<Integer> {
    
        Queue<Integer> q = new LinkedList<>(); 
        
        public PeekingIterator(Iterator<Integer> iterator) {
           while (iterator.hasNext()) q.offer(iterator.next());   
        }
    
        public Integer peek() {
            return q.peek(); 
        }
    
        @Override
        public Integer next() {
    	return q.poll(); 
        }
    
        @Override
        public boolean hasNext() {
    	return (q == null || q.isEmpty()) ? false : true; 
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.