We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d16bd61 commit 655c2b2Copy full SHA for 655c2b2
java/0341-flatten-nested-list-iterator.java
@@ -0,0 +1,27 @@
1
+public class NestedIterator implements Iterator<Integer> {
2
+ Queue<NestedInteger> q; // here we use Queue in place of revesed stack
3
+
4
+ public NestedIterator(List<NestedInteger> nestedList) {
5
+ q = new LinkedList<>();
6
+ dfs(nestedList);
7
+ }
8
9
+ @Override
10
+ public Integer next() {
11
+ return q.poll().getInteger();
12
13
14
15
+ public boolean hasNext() {
16
+ return !q.isEmpty();
17
18
19
+ private void dfs(List<NestedInteger> nestedList){
20
+ for(NestedInteger n : nestedList){
21
+ if(n.isInteger())
22
+ q.add(n);
23
+ else
24
+ dfs(n.getList());
25
26
27
+}
0 commit comments