C语言怎么合并两个有序链表

C语言合并两个有序链表的方法:拼接指定的两个有序链表的所有节点即可。例如两个有序链表分别为【1->2->4】和【1->3->4】,合并后的有序链表为【1->1->2->3->4->4】。

具体方法:

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

输入:

1->2->4, 1->3->4

输出:

1->1->2->3->4->4

分析:两个链表为有序链表,所以依次遍历两个链表比较大小即可。

代码实现:

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *     int val;

 *     struct ListNode *next;

 * };

 */

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){

    if(l1==NULL){

        return l2;

    }

    if(l2==NULL){

        return l1;

    }

    struct ListNode *l = (struct ListNode*)malloc(sizeof(struct ListNode));

    l->next = NULL;

    struct ListNode *list1 = l1;

    struct ListNode *list2 = l2;

    if(l1->valval){

        l->val=l1->val;

        if(list1->next==NULL){

            l->next=list2;

            return l;

        }

        list1=list1->next;

    }else{

        l->val=l2->val;

        if(list2->next==NULL){

            l->next=list1;

            return l;

        }

        list2=list2->next;

    }

    struct ListNode *list = l;

    while(list1->next!=NULL&&list2->next!=NULL){

        if(list1->val<=list2->val){

            struct ListNode *body = (struct ListNode *)malloc(sizeof(struct ListNode));

            body->val = list1->val;

            body->next = NULL;

            list->next = body;

            list = list->next;

            list1 = list1->next;

        }else{

            struct ListNode *body = (struct ListNode*)malloc(sizeof(struct ListNode));

            body->val=list2->val;

            body->next=NULL;

            list->next=body;

            list=list->next;

            list2=list2->next;

        }

    }

    if(list1->next==NULL){

        while(list2->next!=NULL){

            if(list1->val<=list2->val){

                list->next = list1;

                list = list->next;

                list->next=list2;

                return l;

            }else{

                struct ListNode *body = (struct ListNode*)malloc(sizeof(struct ListNode));

                body->val=list2->val;

                body->next=NULL;

                list->next=body;

                list=list->next;

                list2=list2->next;

            }

        }

    }else{

        while(list1->next!=NULL){

            if(list2->val<=list1->val){

                list->next=list2;

                list=list->next;

                list->next=list1;

                return l;

            }else{

                struct ListNode *body = (struct ListNode*)malloc(sizeof(struct ListNode));

                body->val=list1->val;

                body->next=NULL;

                list->next=body;

                list=list->next;

                list1=list1->next;

            }

        }

    }

    if(list1->next==NULL&&list2->next==NULL){

        if(list1->val<=list2->val){

            list->next = list1;

            list=list->next;

            list->next=list2;

        }else{

            list->next=list2;

            list=list->next;

            list->next=list1;

        }

    }

    return l;

}

声明:

本文于网络整理,版权归原作者所有,如来源信息有误或侵犯权益,请联系我们删除或授权事宜。

C语言怎么合并两个有序链表_第1张图片

你可能感兴趣的:(链表,数据结构,leetcode,spark,二叉树)