- Have to use
getList, otherwise it won’t work.
- Because we call
nextevery time, so we can flatten the
topof the stack at that time.
- Iterate backwards on
topelement, and stop when
this.stack.top.isInteger == true.
- Be careful about
[]. We need an exception check before iterating.
You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
Implement the NestedIterator class:
NestedIterator(List<NestedInteger> nestedList)Initializes the iterator with the nested list
int next()Returns the next integer in the nested list.
boolean hasNext()Returns true if there are still some integers in the nested list and false otherwise.
Your code will be tested with the following pseudocode:
initialize iterator with nestedList
res matches the expected flattened list, then your code will be judged as correct.
Input: nestedList = [[1,1],2,[1,1]]