要求

请实现一个函数,把两个从大到小的有序链表合并成一个链表,新的链表是一个从小到大的有序链表。

struct list
{
int value;
list* next;
};
list * merge (list *list1_head, list*list2_head);

代码

*merge(list *list1_head,list *list2_head)
{
list *newlist = NULL;
list *current = NULL;
while(NULL!=list1_head && NULL!=list2_head)
{
if(list1_head->value > list2_head->value)
{
current = list1_head->next;
list1_head->next = newlist;
newlist = list1_head;
list1_head = current;
}
else
{
current = list2_head->next;
list2_head->next = newlist;
newlist = list2_head;
list2_head = current;
}
}

while(NULL!=list1_head)
{
current = list1_head->next;
list1_head->next = newlist;
newlist = list1_head;
list1_head = current;
}
while(NULL!=list2_head)
{
current = list2_head->next;
list2_head->next = newlist;
newlist = list2_head;
list2_head = current;
}
return newlist;
}