# Tag Archives: iterator

## 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: Given the list [[1,1],2,[1,1]], By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].… Read More »

## Zigzag Iterator

This question is from leetcode zigzag iterator. Given two 1d vectors, implement an iterator to return their elements alternately. For example, given two 1d vectors: v1 = [1, 2] v2 = [3, 4, 5, 6] By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2,… Read More »

## Move Zero

This one is from leetcode which I think is very useful. https://leetcode.com/problems/move-zeroes/ Given an array nums, write a function to move all 0‘s to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3,… Read More »

## Peeking Iterator

https://leetcode.com/problems/peeking-iterator/ Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation — it essentially peek() at the element that will be returned by the next call to next(). Here is an example. Assume that the iterator is initialized to the beginning of the list: [1,… Read More »

## Deep Iterator

Write an Iterator that takes a nested(deep) list and flattens it out. For e.g. Collection A = {1, 2, {3, 4, 5, {6, 7}, {8,9}}, 10} When you call DeepIterator it = new DeepIterator(A) ; while(it.hasNext()){ System.out.println(it.next); } The key part of this problem is that the processed data is Collection type, which is the… Read More »