Java AC Solution using Queue...


  • 0
    S
    Queue<Integer> queue;
    	public NestedIterator(List<NestedInteger> nestedList) {
    		queue = new LinkedList<Integer>();
    		makeQueue(nestedList);
        }
    
        private void makeQueue(List<NestedInteger> nestedList) {
        	for(NestedInteger n:nestedList){
            	if(n.isInteger()){
            		queue.add(n.getInteger());
            	}else{
            		makeQueue(n.getList());
            	}
            }
    	}
    
    	@Override
        public Integer next() {
            if(hasNext()){
            	return queue.poll();
            }
            return null;
        }
    
        @Override
        public boolean hasNext() {
            return queue.size()>0;
        }
    

Log in to reply
 

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