Very easy understand Java solution using a queue to store int


  • 0
    F
            private LinkedList<Integer> queue = new LinkedList<Integer>();
    	public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
    		int size = Math.max(v1.size(), v2.size());		
    		for (int i = 0; i < size; i++) {
    			if (i<v1.size())
    				queue.add(v1.get(i));
    			if (i<v2.size())
    				queue.add(v2.get(i));
    		}
    	}
    
    	public int next() {
    		return queue.pop();
    	}
    
    	public boolean hasNext() {
    		return !queue.isEmpty();
    	}
    

Log in to reply
 

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