GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/FlattenNestedListIterator.java at master · LeBW/leetcode

Expand file tree

/

FlattenNestedListIterator.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
59 lines (49 loc) · 1.49 KB
/

FlattenNestedListIterator.java

Copy path

File metadata and controls

59 lines (49 loc) · 1.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 341. Flatten Nested List Iterator
* DFS
* @author LBW
*/
public class FlattenNestedListIterator implements Iterator<Integer>{
private List<Integer> values = new ArrayList<>();
private Iterator<Integer> it;
public FlattenNestedListIterator(List<NestedInteger> nestedList) {
traverse(nestedList);
it = values.iterator();
}
@Override
public Integer next() {
return it.next();
}
@Override
public boolean hasNext() {
return it.hasNext();
}
private void traverse(List<NestedInteger> nestedList) {
for (NestedInteger ni: nestedList) {
dfs(ni);
}
}
private void dfs(NestedInteger ni) {
if (ni.isInteger()) {
values.add(ni.getInteger());
}
else {
for (NestedInteger nni: ni.getList()) {
dfs(nni);
}
}
}
}
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();
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.