forked from seanprashad/leetcode-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path284_Peeking_Iterator.java
34 lines (28 loc) · 774 Bytes
/
284_Peeking_Iterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
classPeekingIteratorimplementsIterator<Integer> {
privateIntegernext;
privateIterator<Integer> it;
publicPeekingIterator(Iterator<Integer> iterator) {
it = iterator;
next = it.next();
}
// Returns the next element in the iteration without advancing the iterator.
publicIntegerpeek() {
returnnext;
}
// hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
publicIntegernext() {
Integerresult = next;
if (it.hasNext()) {
next = it.next();
} else {
next = null;
}
returnresult;
}
@Override
publicbooleanhasNext() {
returnnext != null;
}
}