LC: 281. Zigzag Iterator - spiralgo/algorithms GitHub Wiki

281. Zigzag Iterator

The Essence:

  • The solution requires the creation of a data structure that can handle which list and index currently pointed at.
  • Such a data structure should answer the queries next and hasNext efficiently.
  • If the current list does not have as many elements as the current index points, the data structure should keep returning from the other array.

Details:

The list and index pointers can just be integers, with the list pointer going back and forth between 0 and 1. The index pointer should increase when an element is returned, or when hasNext is called.

For the follow-up problem of extending the algorithm to multiple list, one can use queue for prioritizing the lists or keep which sets are exhausted in an array or a set.