7 ms easy Java solution


  • 0
    C

    public class NestedIterator implements Iterator<Integer> {
    private List<Integer> elements = new ArrayList<Integer>();
    private Iterator<Integer> it;
    public NestedIterator(List<NestedInteger> nestedList) {
    addElements(nestedList);
    it = elements.iterator();
    }
    private void addElements(List<NestedInteger> nestedList){
    for(NestedInteger ni : nestedList){
    if(ni.isInteger())
    elements.add(ni.getInteger());
    else{
    addElements(ni.getList());
    }
    }
    }
    @Override
    public Integer next() {
    return it.next();
    }

    @Override
    public boolean hasNext() {
        return it.hasNext();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.