Skip to content

Instantly share code, notes, and snippets.

@snluu
Created July 25, 2012 07:07
Show Gist options
  • Save snluu/3174862 to your computer and use it in GitHub Desktop.
Save snluu/3174862 to your computer and use it in GitHub Desktop.
void MergeListImproved(ref List<int> list1, List<int> list2)
{
int list1Size = list1.Count;
int list2Size = list2.Count;
int totalSize = list1Size + list2Size;
var newList = new List<int>(totalSize);
int index1 = 0;
int index2 = 0;
while (index1 < list1Size && index2 < list2Size)
if (list1[index1] < list2[index2])
{
newList.Add(list1[index1]);
index1++;
}
else
{
newList.Add(list2[index2]);
index2++;
}
while (index1 < list1Size)
{
newList.Add(list1[index1]);
index1++;
}
while (index2 < list2Size)
{
newList.Add(list2[index2]);
index2++;
}
list1.Clear();
list1.Capacity = totalSize;
list1.AddRange(newList);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment