-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlattenNestedListIterator.java
More file actions
152 lines (134 loc) · 4.54 KB
/
FlattenNestedListIterator.java
File metadata and controls
152 lines (134 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
private final Deque<NestedInteger> stack = new ArrayDeque<>();
public NestedIterator(List<NestedInteger> nestedList) {
ListIterator<NestedInteger> iter = nestedList.listIterator(nestedList.size());
while (iter.hasPrevious()) {
stack.push(iter.previous());
}
advanceToNextInteger();
}
private void advanceToNextInteger() {
while (!stack.isEmpty()) {
NestedInteger top = stack.peek();
if (top.isInteger()) {
return;
} else {
stack.pop();
List<NestedInteger> nestedList = top.getList();
ListIterator<NestedInteger> iter = nestedList.listIterator(nestedList.size());
while (iter.hasPrevious()) {
stack.push(iter.previous());
}
}
}
}
@Override
public Integer next() {
Integer ret = stack.pop().getInteger();
advanceToNextInteger();
return ret;
}
@Override
public boolean hasNext() {
return !stack.isEmpty();
}
}
//Best implementation. Without copying all the nodes. O(h) space
//This is what a real iterator should be implemented.
//https://discuss.leetcode.com/topic/41870/real-iterator-in-python-java-c
public class NestedIterator implements Iterator<Integer> {
private final Deque<ListIterator<NestedInteger>> stack = new ArrayDeque<>();
public NestedIterator(List<NestedInteger> nestedList) {
if (nestedList != null) {
stack.push(nestedList.listIterator());
}
advanceToNextInteger();
}
private void advanceToNextInteger() {
while (!stack.isEmpty()) {
if (!stack.peek().hasNext()) {
stack.pop();
} else {
ListIterator<NestedInteger> iter = stack.peek();
NestedInteger cur = iter.next();
if (cur.isInteger()) {
iter.previous();
return;
}
stack.push(cur.getList().listIterator());
}
}
}
@Override
public Integer next() {
Integer ret = stack.peek().next().getInteger();
advanceToNextInteger();
return ret;
}
@Override
public boolean hasNext() {
return !stack.isEmpty();
}
//Second try, using Iterator and cached next value:
private final Deque<Iterator<NestedInteger>> stack;
private Integer nextInteger = null;
public NestedIterator(List<NestedInteger> nestedList) {
stack = new ArrayDeque<>();
stack.push(nestedList.iterator());
advanceIterator();
}
private void advanceIterator() {
if (!hasNext()) {
for(; !stack.isEmpty(); ) {
Iterator<NestedInteger> iter = stack.peek();
if (!iter.hasNext()) {
stack.pop();
} else {
NestedInteger ni = iter.next();
Integer nint = ni.getInteger();
if (nint != null) {
nextInteger = nint;
return;
}
stack.push(ni.getList().iterator());
}
}
}
}
@Override
public Integer next() {
if (!hasNext()) {
throw new java.util.NoSuchElementException();
}
Integer ret = nextInteger;
nextInteger = null;
advanceIterator();
return ret;
}
@Override
public boolean hasNext() {
return nextInteger != null;
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/