Skip to content

Practice Flatten Nested List Iterator

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#341
LeetCode Problem

Flatten Nested List Iterator

Problem Statement

Implement NestedIterator(nestedList) for a list whose elements are integers or recursively nested lists. next() returns the next integer in left-to-right depth-first order. hasNext() returns whether an unconsumed integer remains. Repeatedly calling next while hasNext is true must produce the same sequence as fully flattening the input; empty nested lists contribute no values.

Source: Flatten Nested List Iterator on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nestedList = [[1,1],2,[1,1]]
Output
[1,1,2,1,1]

# Example 2

Input
nestedList = [1,[4,[6]]]
Output
[1,4,6]

Constraints

1 <= nestedList.length <= 500
The values of the integers in the nested list is in the range [-10^6, 10^6].

Related LeetCode Problems

Practice more problems similar to Flatten Nested List Iterator.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.