forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_341.java
44 lines (34 loc) · 1.2 KB
/
_341.java
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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.NestedInteger;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class _341 {
public static class Solution1 {
public static class NestedIterator implements Iterator<Integer> {
private Queue<Integer> flattenedList;
public NestedIterator(List<NestedInteger> nestedList) {
flattenedList = new LinkedList<>();
constructList(nestedList);
}
private void constructList(List<NestedInteger> nestedList) {
for (NestedInteger nestedInteger : nestedList) {
if (nestedInteger.isInteger()) {
flattenedList.add(nestedInteger.getInteger());
} else {
constructList(nestedInteger.getList());
}
}
}
@Override
public Integer next() {
return flattenedList.poll();
}
@Override
public boolean hasNext() {
return !flattenedList.isEmpty();
}
}
}
}