This method merges two sorted lists and I want to know the time complexity of it if the length of list a is n and the length of list b is m. I am confused with while loops because they also sort of act like if statements (are only executed when the condition is true), meaning they aren't necessarily executed, so how can I compute the time complexity of them?
ArrayList<Integer> union(ArrayList<Integer> a, ArrayList<Integer> b){
ArrayList<Integer> res = new ArrayList<>();
int i = 0, j = 0;
while (i<a.size() && j<b.size()){
if(a.get(i) < b.get(j)){
res.add(a.get(i));
i++;
} else if (a.get(i) > b.get(j)){
res.add(b.get(j));
j++;
} else {
res.add(b.get(j));
i++;
j++;
}
}
while (i < a.size()){
res.add(a.get(i));
i++;
}
while (j < b.size()){
res.add(b.get(j));
j++;
}
return res;
}