Very Clean Java Solution Using Iterators

  • 1

    This question is very good! Even though I can solve much harder algorithmic problems, I have so hard time solving this problem!!!!!!!

    It is very easy to lose the invariants unless carefully thinking about it and any boundary cases.

    Below is my answer, which may look the code better.

    public class Vector2D {
        Iterator<List<Integer>> lItr;
        Iterator<Integer> iItr;
        public Vector2D(List<List<Integer>> vec2d) {
            lItr = vec2d.iterator();
            iItr = lItr.hasNext() ? : null;
        public int next() {
        public boolean hasNext() {
                return false;
            else if(iItr.hasNext())
                return true;
            else {
                while(lItr.hasNext()) {
                    iItr =;
                        return true;
                return false;

  • 0

    upvoted... but this is not that hard at all, lol...

    below is my implementation, which is a little different from yours.

Log in to reply

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