publicNestedIterator(List<NestedInteger> nestedList){ stack = new Stack<>(); if (nestedList == null || nestedList.isEmpty()) return; for (int i = nestedList.size() - 1; i >= 0; i--) { stack.push(nestedList.get(i)); } }
@Override public Integer next(){ if (hasNext()) return stack.pop().getInteger(); returnnull; }
@Override publicbooleanhasNext(){ while (!stack.isEmpty()) { NestedInteger ni = stack.peek(); if (ni.isInteger()) returntrue; stack.pop(); List<NestedInteger> list = ni.getList(); for (int i = list.size() - 1; i >= 0; i--) { stack.push(list.get(i)); } } returnfalse; } }