Last active
August 3, 2023 19:57
-
-
Save rinster/65b766f0144321436e3aede4a8afffff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DeepNestedIterator { | |
| constructor(nestedList) { | |
| this.data = [] | |
| // Recursively precomputes all data at | |
| // the moment the class is instantiated | |
| this.flatten(nestedList) | |
| }; | |
| flatten(list) { | |
| for (let el of list) | |
| if (Number.isInteger(el)) this.data.push(el) | |
| else this.flatten(el) | |
| }; | |
| hasNext() { return this.data.length }; | |
| next() { return this.data.shift() }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment