#codingexercise
Merge two sorted lists:
List<int> merge(List<int> A, List<int> B)
{
List<int> result = new ArrayList<int>();
While (A != null && B != null && A.size() > 0 && B.size() > 0)
{
int min = A.get(0) <= B.get(0) ? A.remove(0) : B.remove(0);
result.add(min);
}
While (A != null && A.size() > 0)
{
result.add(A.remove(0));
}
While(B != null && B.size() > 0)
{
result.add(B.remove(0));
}
return result;
}
Test cases:
Null, Null -> []
Null, [] -> []
[], Null -> []
[],[] -> []
[1],[] -> [1]
[], [1] -> [1]
[1],[1] -> [1, 1]
[1],[2] -> [1, 2]
[2],[1] -> [1,2]
[1],[2,3] -> [1,2,3]
[1,2],[3] -> [1,2,3]
[1,2,3],[] -> [1,2,3]
[1,3][2] -> [1,2,3]
[],[1,2,3] -> [1,2,3]
No comments:
Post a Comment