v1 = [1, 2]
v2 = [3, 4, 5, 6]
public class ZigzagIterator {
/**
* @param v1 v2 two 1d vectors
*/
int len1 = 0;
int i = 0;
int len2 = 0;
int j = 0;
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
len1 = v1.size();
len2 = v2.size();
list1 = v1;
list2 = v2;
}
public int next() {
int res = -1;
if (i < len1 && j < len2) {
if (i <= j) {
res = list1.get(i);
i++;
} else {
res = list2.get(j);
j++;
}
} else if (i == len1 && j < len2) {
res = list2.get(j);
j++;
} else if (i < len1 && j == len2) {
res = list1.get(i);
i++;
}
return res;
}
public boolean hasNext() {
if (i < len1 || j < len2) {
return true;
}
return false;
}
}