DIY: Flatten Nested List Iterator
Solve the interview question "Flatten Nested List Iterator" in this lesson.
We'll cover the following
Problem statement
You will be given a nested list of integers named nestedList
. Each element will either be an integer or a list whose elements may also be integers or other lists. Your task will be to implement an iterator to flatten the nested list.
You will have to implement the NestedIterator
class. This class has the following functions:
NestedIterator(List<NestedInteger> nestedList)
, which will initialize the iterator with the nested listnestedList
.int next()
, which will return the next integer in the nested list.boolean hasNext()
, which will returntrue
if there are still some integers in the nested list and, otherwise, will returnfalse
.
You will be given a class definition for NestedInteger
and some helper functions. Make sure to look at them in the NestedInteger.java
file, in the exercise given below.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.