Flatten Nested List Iterator

Medium

Description

Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer or a list whose elements may also be integers or other lists.

Examples

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

Flatten the nested structure.

Input:nestedList = [[1]]
Output:[1]
Explanation:

Minimal case with a single-element matrix.

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

This example demonstrates deep nesting where lists are nested multiple levels deep. The iterator must recursively flatten [4,[6]] to get 4,6, then combine with the top-level integer 1 to produce the final flattened sequence [1,4,6].

Constraints

  • 1 ≤ nestedList.length ≤ 500
  • Values are in range [-10⁶, 10⁶]

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!