严氏北美IT公司面试真题汇总和解答论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Merge Two Sorted List

Go down

Merge Two Sorted List Empty Merge Two Sorted List

Post by Admin Sat Oct 21, 2017 4:53 pm

public List<Integer> mergeTwoList(List<Integer> list1, List<Integer> list2) {
List<Integer> res = new ArrayList<>();
int index1 = 0;
int index2 = 0;
while (index1 < list1.size() && index2 < list2.size()) {
int val1 = list1.get(index1);
int val2 = list2.get(index2);
if (val1 <= val2) {
res.add(val1);
index1++;
}
else {
res.add(val2);
index2++;
}
}
while (index1 < list1.size()) {
res.add(list1.get(index1));
index1++;
}
while (index2 < list2.size()) {
res.add(list2.get(index2));
index2++;
}
return res;
}

Admin
Admin

Posts : 124
Join date : 2017-10-21

https://csinterviewquestions.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum