/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class NestedIterator implements Iterator<Integer> {
private List<NestedInteger> nestedIntegerList;
private NestedIterator nextNestedIterator;
private NestedInteger nextNestedInteger;
private int currentPosition;
public NestedIterator(List<NestedInteger> nestedList) {
this.nestedIntegerList = nestedList;
this.currentPosition = 0;
}
@Override
return nextNestedInteger.isInteger() ? nextNestedInteger.getInteger() :
nextNestedIterator.next();
}
@Override
public boolean hasNext() {
if (nextNestedIterator != null && nextNestedIterator.hasNext()) {
return true;
}
if (currentPosition >= nestedIntegerList.size()) {
return false;
}
nextNestedInteger = nestedIntegerList.get(currentPosition++);
if ( nextNestedInteger.isInteger() ||
(nextNestedIterator =
new NestedIterator(nextNestedInteger.getList())).hasNext()) {
return true;
} else {
return hasNext();
}
}
}