@@ -0,0 +1,2513 @@
+import java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Stack;%0A%0A/**%0A * Given a nested list of integers, implement an iterator to flatten it.%0A * %3Cp%3E%0A * Each element is either an integer, or a list -- whose elements may also be integers or other lists.%0A * %3Cp%3E%0A * Example 1:%0A * Given the list %5B%5B1,1%5D,2,%5B1,1%5D%5D,%0A * %3Cp%3E%0A * By calling next repeatedly until hasNext returns false, the order of elements returned by next should be:%0A * %5B1,1,2,1,1%5D.%0A * %3Cp%3E%0A * Example 2:%0A * Given the list %5B1,%5B4,%5B6%5D%5D%5D,%0A * %3Cp%3E%0A * By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: %5B1,4,6%5D.%0A * %3Cp%3E%0A * Tags: Stack, Design%0A * Similar Problems: (M) Flatten 2D Vector, (M) Zigzag Iterator%0A */%0Apublic class FlattenNestedListIterator %7B%0A%0A    /**%0A     * This is the interface that allows for creating nested lists.%0A     * You should not implement it, or speculate about its implementation%0A     */%0A    public interface NestedInteger %7B%0A%0A        // @return true if this NestedInteger holds a single integer, rather than a nested list.%0A        public boolean isInteger();%0A%0A        // @return the single integer that this NestedInteger holds, if it holds a single integer%0A        // Return null if this NestedInteger holds a nested list%0A        public Integer getInteger();%0A%0A        // @return the nested list that this NestedInteger holds, if it holds a nested list%0A        // Return null if this NestedInteger holds a single integer%0A        public List%3CNestedInteger%3E getList();%0A    %7D%0A%0A    public class NestedIterator implements Iterator%3CInteger%3E %7B%0A%0A        private Stack%3CNestedInteger%3E stack;%0A%0A        public NestedIterator(List%3CNestedInteger%3E nestedList) %7B%0A            stack = new Stack%3C%3E();%0A            flattenList(nestedList);%0A        %7D%0A%0A        @Override%0A        public Integer next() %7B%0A            return hasNext() ? stack.pop().getInteger() : null;%0A        %7D%0A%0A        @Override%0A        public boolean hasNext() %7B%0A            while (!stack.isEmpty()) %7B%0A                if (stack.peek().isInteger()) return true;%0A                flattenList(stack.pop().getList());%0A            %7D%0A            return false;%0A        %7D%0A%0A        private void flattenList(List%3CNestedInteger%3E list) %7B%0A            for (int i = list.size() - 1; i %3E= 0; i--) %7B%0A                stack.push(list.get(i));%0A            %7D%0A        %7D%0A    %7D%0A%0A/**%0A * Your NestedIterator object will be instantiated and called as such:%0A * NestedIterator i = new NestedIterator(nestedList);%0A * while (i.hasNext()) v%5Bf()%5D = i.next();%0A */%0A%7D%0A

add FlattenNestedListIterator