# 两个不交叉的有序链表的合并

2014-09-10 06:30:01 阅读( 55 )

{
//compare until one link run to the end
while (curr1.Next != null && curr2.Next != null)
{
if (curr1.Next.Data < curr2.Next.Data)
{
curr1 = curr1.Next;
}
else
{
curr2 = curr2.Next;
}
pre.Next = curr;
pre = pre.Next;
}
//if head1 run to the end
while (curr1.Next != null)
{
curr1 = curr1.Next;
pre.Next = curr;
pre = pre.Next;
}
//if head2 run to the end
while (curr2.Next != null)
{
curr2 = curr2.Next;
pre.Next = curr;
pre = pre.Next;
}
}

{
//compare until one link run to the intersect
while (curr1.Next != intersect && curr2.Next != intersect)
{
if (curr1.Next.Data < curr2.Next.Data)
{
curr1 = curr1.Next;
}
else
{
curr2 = curr2.Next;
}
pre.Next = curr;
pre = pre.Next;
}
//if head1 run to the intersect
if (curr1.Next == intersect)
{
while (curr2.Next != null)
{
curr2 = curr2.Next;
pre.Next = curr;
pre = pre.Next;
}
}
//if head2 run to the intersect
else if (curr2.Next == intersect)
{
while (curr1.Next != null)
{
curr1 = curr1.Next;
pre.Next = curr;
pre = pre.Next;
}
}
}