Using iterator, actually I don't quite get it, plus it's less frequent problem, let's review it briefly.
public class ZigzagIterator {
Queue<Iterator<Integer>> queue;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
queue = new LinkedList<Iterator<Integer>>();
if (v1.iterator().hasNext()) queue.offer(v1.iterator());
if (v2.iterator().hasNext()) queue.offer(v2.iterator());
}
public int next() {
Iterator<Integer> next = queue.poll();
int result = next.next();
if (next.hasNext()) queue.offer(next);
return result;
}
public boolean hasNext() {
return !queue.isEmpty();
}
}
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/
Using iterator, actually I don't quite get it, plus it's less frequent problem, let's review it briefly.