Easy to understand Java solution


  • 0
    T
    public class ZigzagIterator {
    
        List<Integer> list = new ArrayList<Integer>();
        
        /*
            input: [1,2]
                   [3,4,5,6]
            list --> [1,3,2,4,5,6] 
            keep removing first element.
        */
        
        
        public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
            for(int i=0;i<Math.max(v1.size(),v2.size());i++){
                if(i<v1.size()) list.add(v1.get(i));
                if(i<v2.size()) list.add(v2.get(i));
            }
        }
    
        public int next() {
            int first = list.get(0);
            list.remove(0);
            return first;
        }
    
        public boolean hasNext() {
            return list.size()!=0;
        }
    }
    

Log in to reply
 

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