Flatten Nested List Iterator

LeetCode Q 341 - Flatten Nested List Iterator

Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list – whose elements may also be integers or other lists.

Example 1: Input: [[1,1],2,[1,1]] ; Output: [1,1,2,1,1]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

Example 2: Input: [1,[4,[6]]] ; Output: [1,4,6]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

Solution

The given API

// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
public interface NestedInteger {

	// @return true if this NestedInteger holds a single integer, rather than a nested list.
	public boolean isInteger();

	// @return the single integer that this NestedInteger holds, if it holds a single integer
	// Return null if this NestedInteger holds a nested list
	public Integer getInteger();

	// @return the nested list that this NestedInteger holds, if it holds a nested list
	// Return null if this NestedInteger holds a single integer
	public List<NestedInteger> getList();
}

Solution 1: use Stack to store the NestedInteger

Code:


Deque<NestedIterator> deque;
Deque<Integer> intDeque;

public NestedIterator(List<NestedInteger> nestedList) {
	deque = new ArrayDeque<>(nestedList);
	intDeque = new ArrayDeque<>();
}

@Override
public Integer next() {

	if (!intDeque.isEmpty()) return intDeque.pop();

	if (deque.isEmpty) return null;
	
	NestedInteger curr = deque.pop();
	if (curr.isInteger()) {
		return curr.getInteger();
	} else {
		List<NestedInteger> list = curr.getList();
		for (int i = list.size() - 1; i >= 0; i--) 
			deque.push(list.get(i));
		return next();
	}
}

@Override
public boolean hasNext() {
	Integer ans = next();
	if (ans == null) return false;
	intdeque.push(ans);
	return true;
}

Solution 2: use Stack to store the iterator

This solution is more efficient comparing with solution1.
In the initialization step, we don’t need to push all the nestedList into the stack, instead we can only push its iterator.

Code:


Integer curr;
Deque<Iterator<NestedInteger>> deque;

public NestedIterator(List<NestedInteger> nestedList) {
	deque = new ArrayDeque<>();
	deque.push(nestedList.iterator());
}

@Override
public Integer next() {
	if (!hasNext()) return null;

	Integer ans = curr;
	curr = null;

	return ans;
}

@Override
public boolean hasNext() {

	while (curr == null && !deque.isEmpty()) {
		Iterator<NestedInteger> it = deque.peek();
		if (!it.hasNext()) {
			deque.pop(); continue;
		}

		NestedInteger ni = it.next();
		if (ni.isInteger()) {
			curr = ni.getInteger();
			break;
		} else {
			deque.push(ni.getList().iterator());
		}
	}

	return curr != null;
}

##Deque in Java 8##


   Reprint policy


《Flatten Nested List Iterator》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC