Java solution with Deque (good for beginners only)


  • 0
    U
    public class Vector2D implements Iterator<Integer> {
        Deque<Integer> dq = new LinkedList<>();
        int count ;
        public Vector2D(List<List<Integer>> vec2d) {
            for (List<Integer> i : vec2d) {
                for (int j :i) {
                    dq.offerLast(j);
                }
            }
            count = 0;
        }
    
        @Override
        public Integer next() {
           return dq.pollFirst();
            
        }
    
        @Override
        public boolean hasNext() {
            return !dq.isEmpty();
        }
    }
    
    

Log in to reply
 

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