1
+ /**
2
+ * // This is the interface that allows for creating nested lists.
3
+ * // You should not implement it, or speculate about its implementation
4
+ * function NestedInteger() {
5
+ *
6
+ * Return true if this NestedInteger holds a single integer, rather than a nested list.
7
+ * @return {boolean }
8
+ * this.isInteger = function() {
9
+ * ...
10
+ * };
11
+ *
12
+ * Return the single integer that this NestedInteger holds, if it holds a single integer
13
+ * Return null if this NestedInteger holds a nested list
14
+ * @return {integer }
15
+ * this.getInteger = function() {
16
+ * ...
17
+ * };
18
+ *
19
+ * Return the nested list that this NestedInteger holds, if it holds a nested list
20
+ * Return null if this NestedInteger holds a single integer
21
+ * @return {NestedInteger[] }
22
+ * this.getList = function() {
23
+ * ...
24
+ * };
25
+ * };
26
+ */
27
+ /**
28
+ * @constructor
29
+ * @param {NestedInteger[] } nestedList
30
+ */
31
+ var NestedIterator = function ( nestedList ) {
32
+ this . stack = [ ]
33
+ while ( nestedList . length > 0 ) {
34
+ let d = nestedList . pop ( )
35
+ if ( d . isInteger ( ) )
36
+ this . stack . push ( d . getInteger ( ) )
37
+ else
38
+ nestedList = nestedList . concat ( d . getList ( ) )
39
+ }
40
+ } ;
41
+
42
+
43
+ /**
44
+ * @this NestedIterator
45
+ * @returns {boolean }
46
+ */
47
+ NestedIterator . prototype . hasNext = function ( ) {
48
+ return this . stack . length > 0
49
+ } ;
50
+
51
+ /**
52
+ * @this NestedIterator
53
+ * @returns {integer }
54
+ */
55
+ NestedIterator . prototype . next = function ( ) {
56
+ return this . stack . pop ( )
57
+ } ;
58
+
59
+ /**
60
+ * Your NestedIterator will be called like this:
61
+ * var i = new NestedIterator(nestedList), a = [];
62
+ * while (i.hasNext()) a.push(i.next());
63
+ */
0 commit comments