Share my java solution


  • 0
    M

    public class ZigzagIterator {

    boolean isV1;
    Iterator<Integer> i1,i2;
    public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
    i1 = v1.iterator();
    i2 = v2.iterator();

        isV1 = i1.hasNext();
    }
    
    public int next() {
        if(isV1){
            isV1 = i2.hasNext()?false:true;
            return i1.next();
        }else{
            isV1 = i1.hasNext()?true:false;
            return i2.next();
        }    
    }
    
    public boolean hasNext() {
        return i1.hasNext() || i2.hasNext();
    }
    

    }


Log in to reply
 

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