如何将两个有序单链表合并为一个有序单链表
public static LinkedList<int> MergeLink(LinkedList<int> link1, LinkedList<int> link2)
{
LinkedListNode<int> currentNodeOfLink1 = link1.First;
LinkedListNode<int> currentNodeOfLink2 = link2.First;
while (currentNodeOfLink2.Next != null)
{
if (currentNodeOfLink1.Value >= currentNodeOfLink2.Value)
{
if (currentNodeOfLink1.Next != null)
{
currentNodeOfLink1 = currentNodeOfLink1.Next;
}
else
{
link1.AddAfter(currentNodeOfLink1, currentNodeOfLink2);
while (currentNodeOfLink2.Next != null)
{
currentNodeOfLink2 = currentNodeOfLink2.Next;
}
}
}
else
{
link1.AddBefore(currentNodeOfLink1, currentNodeOfLink2);
while (currentNodeOfLink2.Next != null)
{
currentNodeOfLink2 = currentNodeOfLink2.Next;
}
}
}
return link1;
}