static List<int>Merge(List<int> list_a, List<int> list_b)
{
List<int> list_c=new List<int>();
int countA = 0, countB = 0;
for (int i =0;i< list_a.Count + list_b.Count-2;i++)
{
if (list_a[countA]<=list_b[countB])
{
list_c.Add(list_a[countA]);
countA ++;
}
else
{
list_c.Add(list_b[countB]);
countB ++;
}
}
return list_c;
}
my idea was to go through the for loop as many times as how many element list_c will have at the end Compare each element in both list then add the smallest one in list_c i already have a way to check if both lists are in ascending order when im testing it with
List<int> myList1 = new List<int> { 1, 2, 3, 7, 8, 9 };
List<int> myList2 = new List<int> { 4, 5, 6};
Console.WriteLine("new list :{ " + string.Join(",", Merge(myList1, myList2)));
countB goes out of bound once the last element in list b is added, the next comparison in that for-loop is then invalid as its comparing list_b[3]