Really simple Java solution using a list

  • 0

    Add every item to a list and maintain its iterator:

    Iterator<Integer> itr;
    public NestedIterator(List<NestedInteger> nestedList) {
        List<Integer> q = new ArrayList<Integer>();
        for (NestedInteger i : nestedList) {
            if (i.isInteger()) q.add(i.getInteger());
            else               q.addAll(i.getList());
        itr = q.iterator();
    public Integer next() {
    public boolean hasNext() {
        return itr.hasNext();

Log in to reply

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