Why can't we use iter.hasNext() to check if peekingiteration.hasNext()?
I am confusing is it here a c-style casting which makes a copy of Iterator and doesn't modify the original this pointer?
Copy constructor itself doesn't return anything.
No one has replied
Unless you use the same vector to init two iterators, which is not in the requirement of the question.
Can someone explain me the difference between
it.next() in peek and Iterator::next() in next() function of PeekingIterator class?
What I understand is that Iterator::next() makes the iterator go forward where as it.next() returns the next value.
But I don't understand why this happens.
hi, @zahid2 , i think " cache = it.next();" in the constructor is buggy, since it.hasNext() may be false. And when we are constructing the iterator, we are not checking with hasNext() yet
Oh sorry I mistakenly interpreted the checkNotNull() method. Now I stick to the original semantic. Thank you for pointing out!
Can anyone explain how iterator.next() and iterator.hasNext() work if n == null?
Hi luantian, I'm I don't understand your question. Can you please elaborate more if you are still interested in knowing the answer?
This solution doesn't handle null value -- Integers in iterator could be null.
I believe leetcode updated its test cases.
thanks for the quick response. Now I understand, it calls the hasnext function from the class iterator, not from peekIterator.
Hi @haoyu2, in my constructor function, peekElement would be initialized as the first element of the input iterator if it is not empty.
@StefanPochmann How does the compiler identify which hasNext() to call?
When the input has a value of INT_MAX or INT_MIN or other value that you used as mark, the method is no longer correct.
@ayuanx you are right. Instead of using -1, a flag should be used. I don't think there is a one variable solution.
Disabled Categories are greyed out
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.