Sharing my ArrayList code 97%


  • 0
    2
    public class NestedIterator implements Iterator<Integer> {
    
        private List<Integer> rootList;
        private int index;
        public NestedIterator(List<NestedInteger> nestedList) {
            rootList = new ArrayList<>();
            for (NestedInteger i : nestedList) {
                helper(i);
            }
            index = 0;
        }
        private void helper(NestedInteger i) {
            if (i.isInteger()) {
                rootList.add(i.getInteger());
            } else {
                for (NestedInteger j : i.getList()) {
                    helper(j);
                }
            }
        }
    
        @Override
        public Integer next() {
            return rootList.get(index++);
        }
    
        @Override
        public boolean hasNext() {
            if (index >= rootList.size()) {
                return false;
            } else {
                return true;
            }
        }
    }
    

Log in to reply
 

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