341. Flatten Nested List Iterator
Flatten Nested List Iterator (LeetCode 341) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
How to solve Flatten Nested List Iterator
Flatten Nested List Iterator is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
Work towards the solution in these steps:
- Consider how you might represent or traverse nested structures to access all elements in a sequential manner.
- Think about using a stack data structure to manage the nested lists and integers as you traverse them.
- When encountering a nested list on the stack, consider how you can expand it to continue flattening without losing track of your position.
The reference solution runs in O(N) time and O(D) space.
The Flatten Nested List Iterator solution and editorial walks through the full approach with reference code.
Problem statement
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
- The values of the integers in the nested list is in the range [-10^6, 10^6].
Topics and companies
Topics: Stack, Tree, Depth-First Search, Design, Queue, Iterator.
Reported in interviews at Facebook, Amazon, Airbnb, Apple, LinkedIn, Bloomberg, Oracle.
How to practise Flatten Nested List Iterator on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(D) space.
Open Flatten Nested List Iterator in the code editor, or read the Flatten Nested List Iterator editorial for a worked solution with its approach and complexity analysis.
Where Flatten Nested List Iterator sits in the DSA pattern sheet
Problems related to Flatten Nested List Iterator
Other problems that use the same Design (General/Specific) and Design Patterns techniques:
- All O`one Data Structure — Hard, same subpattern
- Design a Stack With Increment Operation — Medium, same subpattern
- Design a Text Editor — Hard, same subpattern
- Design Circular Deque — Medium, same subpattern
- Design Circular Queue — Medium, same subpattern
- Design Compressed String Iterator — Easy, same subpattern
Flatten Nested List Iterator FAQ
What is the LeetCode number of Flatten Nested List Iterator?
Flatten Nested List Iterator is LeetCode problem 341, rated Medium.
What pattern is used to solve Flatten Nested List Iterator?
Flatten Nested List Iterator is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
What is the time complexity of Flatten Nested List Iterator?
The reference solution runs in O(N) time and O(D) space.
Which companies ask Flatten Nested List Iterator?
Flatten Nested List Iterator has been reported in interviews at Facebook, Amazon, Airbnb, Apple, LinkedIn, Bloomberg, Oracle.