281. Zigzag Iterator

Problem

Design an iterator that iterates over two vectors in zigzag order (alternating between them).

Given two vectors (or lists) v1 and v2, design an iterator that returns elements in zigzag order: one element from v1, one from v2, one from v1, one from v2, and so on.

If one vector runs out, continue with the remaining elements from the other vector.

Implement the ZigzagIterator class:

  • ZigzagIterator(v1, v2) initializes the iterator with the two vectors.
  • next() returns the next value in zigzag order and advances the iterator. If both vectors are exhausted, return -1.
  • hasNext() returns true if at least one value remains, otherwise false.
Examples
Input: [["ZigzagIterator","next","next","next","next","next","next"],[[[1,2],[3,4,5,6]],[],[],[],[],[],[]]]
Output: [null,1,3,2,4,5,6]
Hints
Related Problems

281. Zigzag Iterator

Design an iterator that iterates over two vectors in zigzag order (alternating between them).