Simple Java Solution using a Queue and peek method

  • 0
    public class Vector2D implements Iterator<Integer> {
    Queue<Iterator> list = new LinkedList<Iterator>();
    public Vector2D(List<List<Integer>> vec2d) {
        for(List<Integer> row : vec2d) {
    public Integer next() {
        int temp = (int)list.peek().next();
        if(!list.peek().hasNext()) list.poll();
        return temp;
    public boolean hasNext() {
        return !list.isEmpty(); 

Log in to reply

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