//有序单链表合并
Node *MergeList(Node *head1, Node *head2)
{
Node *p1 = NULL;
Node *p2 = NULL;
Node *head = NULL;
if (head1->data < head2->data)
{
head = head1;
p1 = head1->next;
p2 = head2->next;
}
else
{
head = head2;
p2 = head2->next;
p1 = head1->next;
}
Node *pre = head;
while (p1 != NULL && p2 != NULL)
{
if (p1->data < p2->data)
{
pre->next = p1;
pre = p1;
p1 = p1->next;
}
else
{
pre->next = p2;
pre = p2;
p2 = p2->next;
}
}
if (p1 != NULL)
{
pre->next = p1;
}
if (p2 != NULL)
{
pre->next = p2;
}
return head;
}
阅读(428) | 评论(0) | 转发(0) |