Java Queue Solution


  • 0
    S
    Queue<Integer> queue;
    
    public NestedIterator(List<NestedInteger> nestedList) {
        queue = new LinkedList<Integer>();
        flatten(nestedList);
    }
    
    public Integer next() {
        return queue.poll();
    }
    
    public boolean hasNext() {
        return !queue.isEmpty();
    }
    
    public void flatten(List<NestedInteger> nestlst){
        for(NestedInteger i:nestlst){
            if(i.isInteger())
                queue.add(i.getInteger());
            else
                flatten(i.getList());
        }
    }

Log in to reply
 

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