2
* // This is the interface that allows for creating nested lists.3
* // You should not implement it, or speculate about its implementation4
* function NestedInteger() {6
* Return true if this NestedInteger holds a single integer, rather than a nested list.8
* this.isInteger = function() {12
* Return the single integer that this NestedInteger holds, if it holds a single integer13
* Return null if this NestedInteger holds a nested list15
* this.getInteger = function() {19
* Return the nested list that this NestedInteger holds, if it holds a nested list20
* Return null if this NestedInteger holds a single integer21
* @return {NestedInteger[]}22
* this.getList = function() {29
* @param {NestedInteger[]} nestedList31
var NestedIterator = function (nestedList) {34
const dfs = (elem) => {35
if (!elem.isInteger()) {36
let list = elem.getList();37
for (let i = 0; i < list.length; i++) {41
this.nestedList.push(elem.getInteger());44
for (let i = 0; i < nestedList.length; i++) {50
* @this NestedIterator53
NestedIterator.prototype.hasNext = function () {54
if (this.ptr < this.nestedList.length) return true;59
* @this NestedIterator62
NestedIterator.prototype.next = function () {63
return this.nestedList[this.ptr++];67
* Your NestedIterator will be called like this:68
* var i = new NestedIterator(nestedList), a = [];69
* while (i.hasNext()) a.push(i.next());