Coding:两个从大到小的有序链表合并成一个从小到大有序链表

要求

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

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

代码

    list *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;
    }
相关推荐
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页