# Simple Java solution for K-vector

• Uses a linkedlist to store the iterators in different vectors. Every time we call next(), we pop an element from the list, and re-add it to the end to cycle through the lists.

``````public class ZigzagIterator {
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
}

public int next() {
Iterator poll = list.remove();
int result = (Integer)poll.next();
return result;
}

public boolean hasNext() {
return !list.isEmpty();
}
}``````

• Iterator poll = list.remove(0);

• Very nice solution, for better, list can be declared as Queue<Iterator> list; and use Queue's interface function (poll & offer)

• Use Deque.

Deque<Iterator> queue;

• Great solution. This is so easy to extend to k-list case. Here I use a Queue interface taking @pinkfloyda's suggestion.

``````public class ZigzagIterator {

// Better solution, 6ms
Queue<Iterator> q;

public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
if (!v1.isEmpty()) q.offer(v1.iterator());
if (!v2.isEmpty()) q.offer(v2.iterator());
}

public int next() {
Iterator cur = q.poll();
int res = (int) cur.next();
if (cur.hasNext()) q.offer(cur);
return res;
}

public boolean hasNext() {
return q.peek() != null;
}
}

/* Previous Solution 4ms 72%

public class ZigzagIterator {
int i;
boolean odd;
List<Integer> l1, l2;

public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
i = 0;
odd = false;
l1 = v1; l2 = v2;
}

public int next() {
odd ^= true;
if (odd && i < l1.size()) {
if (i < l2.size())
return l1.get(i);
else {
odd ^= true;
return l1.get(i++);
}

}
if (i < l2.size()) return l2.get(i++);
return next();
}

public boolean hasNext() {
return i < l1.size() || i < l2.size();
}
}
*/

/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/``````

• Very nice BFS like solution. It can be easily extended to k-vector input.

• Great Solution. I prefer Deque.

``````public class ZigzagIterator {
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
if (!v1.isEmpty()) {
queue.offer(v1.iterator());
}
if (!v2.isEmpty()) {
queue.offer(v2.iterator());
}
}

public int next() {
Iterator<Integer> i = queue.poll();
int ans = i.next();
if (i.hasNext()) {
queue.offer(i);
}
return ans;
}

public boolean hasNext() {
return !queue.isEmpty();
}
}
``````

• @kevinhsu sorry to bother you, i have a stupid question.
when we use list.add(poll), poll will not contain the element "result"?
Does it means that iterator will remove the element when we use poll.next()?

• Share:

``````Iterator<Integer> iter1;
Iterator<Integer> iter2;
int iterIdx = 1;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
iter1 = v1.iterator();
iter2 = v2.iterator();
}

public int next() {
if(iterIdx == 1){
iterIdx = 2;
return iter1.next();
} else {
iterIdx = 1;
return iter2.next();
}
}

public boolean hasNext() {
if(iterIdx == 1){
if(iter1.hasNext()) return true;
else {
iterIdx = 2;
return iter2.hasNext();
}
} else {
if(iter2.hasNext()) return true;
else {
iterIdx = 1;
return iter1.hasNext();
}
}
}``````

• Very nice! Much easier than mine. Thanks for sharing!

• One simple question, why do we need to cast the output to (int) after calling .next()? From my opinion, the type of iterator we use are all Integer.

• @al9 no need. refer to api

• You must be an experienced coder.

• @freezaku I have same confusion. Why next iteration, when set list.remove() to a new iterator poll, poll.next() will automatically point to next element?

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