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 nestedList
.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 = [] while iterator.hasNext() append iterator.next() to the end of res return res
If res
matches the expected flattened list, then your code will be judged as correct.
Example 1:
Input: nestedList = [[1,1],2,[1,1]] Output: [1,1,2,1,1] Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
Example 2:
Input: nestedList = [1,[4,[6]]] Output: [1,4,6] Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
Constraints:
1 <= nestedList.length <= 500
[-106, 106]
.This problem requires creating an iterator that flattens a nested list of integers. The solution uses a depth-first search (DFS) approach to traverse the nested structure and collect all integers into a single list. This list is then used by the iterator to provide the next()
and hasNext()
functionality.
Depth-First Search (DFS): A recursive helper function dfs
is used to traverse the nested list. It checks each element:
nums
list.dfs
function is called recursively on that sublist.Iterator Implementation: The NestedIterator
class implements the iterator interface.
__init__(nestedList)
: Initializes the iterator by calling dfs
to flatten the input list.next()
: Returns the next integer from the flattened nums
list. It increments the internal index i
.hasNext()
: Checks if there are more integers remaining in the nums
list by comparing the index i
with the list's length.Time Complexity: O(N), where N is the total number of integers in the nested list. The DFS traversal visits each integer exactly once.
Space Complexity: O(N) in the worst case, where the nested list is a deeply nested structure with many integers. This space is used to store the flattened list nums
and the recursion stack during the DFS traversal. In the best case (a flat list), the space complexity is O(1) excluding the input list.
The Python code directly implements the described approach. The dfs
function is a helper function that recursively traverses the nested lists. The NestedIterator
class then uses this flattened list to implement the iterator. Note that the problem definition includes a NestedInteger
class definition, which is used in the code but is not fully implemented as it is part of the problem's interface and would require custom implementation based on the specific environment (LeetCode, etc.)
The Java, C++, Go, TypeScript, and Rust code examples follow the same overall approach. They differ in syntax and specific features of each language, but the core logic of the DFS and iterator implementation remains consistent. For example:
ArrayList
for nums
and implements the Iterator
interface.vector
for nums
and implements the iterator functionality explicitly.[]int
) for nums
. The constructor is explicitly named Constructor
.number[]
for nums
and directly implements hasNext()
and next()
.Vec<i32>
for nums
and implements the iterator using the Iterator
trait.The provided code examples demonstrate a clear, efficient, and well-structured solution to the nested list iterator problem. The use of recursion through DFS simplifies the flattening process, and the separate iterator class provides a clean and organized implementation.