8000 [N-0] refactor 341 · kanjain/Leetcode@483dd5f · GitHub
[go: up one dir, main page]

Skip to content

Commit 483dd5f

Browse files
[N-0] refactor 341
1 parent 3014298 commit 483dd5f

File tree

1 file changed

+11
-16
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+11
-16
lines changed

src/main/java/com/fishercoder/solutions/_341.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,25 @@
1111
* 341. Flatten Nested List Iterator
1212
*
1313
* Given a nested list of integers, implement an iterator to flatten it.
14-
15-
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
16-
17-
Example 1:
18-
Given the list [[1,1],2,[1,1]],
19-
20-
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
21-
22-
Example 2:
23-
Given the list [1,[4,[6]]],
24-
25-
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
14+
* Each element is either an integer, or a list -- whose elements may also be integers or other lists.
15+
*
16+
* Example 1:
17+
* Given the list [[1,1],2,[1,1]],
18+
* By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
19+
*
20+
* Example 2:
21+
* Given the list [1,[4,[6]]],
22+
* By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
2623
*/
2724
public class _341 {
2825

2926
public static class NestedIterator implements Iterator<Integer> {
3027

3128
private Queue<Integer> flattenedList;
32-
private Iterator<Integer> iterator;
3329

3430
public NestedIterator(List<NestedInteger> nestedList) {
3531
flattenedList = new LinkedList<>();
3632
constructList(nestedList);
37-
iterator = flattenedList.iterator();
3833
}
3934

4035
private void constructList(List<NestedInteger> nestedList) {
@@ -49,12 +44,12 @@ private void constructList(List<NestedInteger> nestedList) {
4944

5045
@Override
5146
public Integer next() {
52-
return iterator.next();
47+
return flattenedList.poll();
5348
}
5449

5550
@Override
5651
public boolean hasNext() {
57-
return iterator.hasNext();
52+
return !flattenedList.isEmpty();
5853
}
5954
}
6055

0 commit comments

Comments
 (0)
0