# 341. Flatten Nested List Iterator

[Link](https://leetcode.com/problems/flatten-nested-list-iterator/)

**Solution**

這題我是DFS解法

```
public class NestedIterator implements Iterator<Integer> {

    Queue<Integer> list;
    public NestedIterator(List<NestedInteger> nestedList) {
        list = new LinkedList<>();
        dfs(nestedList, list);
    }
    
    public void dfs(List<NestedInteger> nestedList, Queue<Integer> list){
        
        for(NestedInteger nestInt : nestedList){
            if(nestInt.isInteger()){
                list.add(nestInt.getInteger());
            }
            else{
                dfs(nestInt.getList(), list);
            }
        }
    }
    

    @Override
    public Integer next() {
        return list.poll();
    }

    @Override
    public boolean hasNext() {
        return !list.isEmpty();
    }
}
```
