Java | Leetcode Java题解之第284题窥视迭代器

avatar
作者
猴君
阅读量:0

题目:

题解:

class PeekingIterator<E> implements Iterator<E> {     private Iterator<E> iterator;     private E nextElement;      public PeekingIterator(Iterator<E> iterator) {         this.iterator = iterator;         nextElement = iterator.next();     }          public E peek() {         return nextElement;     }          @Override     public E next() {         E ret = nextElement;         nextElement = iterator.hasNext() ? iterator.next() : null;         return ret;     }          @Override     public boolean hasNext() {         return nextElement != null;     } }

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!