题目:
题解:
class PeekingIterator implements Iterator { private Iterator iterator; private E nextElement; public PeekingIterator(Iterator 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; } }
下一篇:LeetCode160 相交链表