当前位置 : 主页 > 编程语言 > c语言 >

[Leetcode]合并两个有序链表

来源:互联网 收集:自由互联 发布时间:2023-09-07
力扣链接 依次比较,取小的尾插: 初步代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){

力扣链接

[Leetcode]合并两个有序链表_链表  合并

依次比较,取小的尾插:








初步代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* head = NULL,*tail = NULL;
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;
    while(cur1 && cur2)
    {
        if(cur1->val<cur2->val)
        {
            if(head == NULL)
            {
                head = tail = cur1;//为空直接赋值
            }
            else
            {
                 tail->next = cur1;
                 tail = tail->next;

            }
           
            cur1 = cur1->next;
            
        }
        else
        {
            if(head == NULL)
            {
                head = tail = cur2;
            }
            else
            {
                 tail->next = cur2;
                 tail = tail->next;

            }
           
            cur2 = cur2->next;

        }
    
    }
    if(cur2)
    {
        tail->next = cur2;
    }
    if(cur1)
    {
        tail->next = cur1;
    }
    return head;


}

[Leetcode]合并两个有序链表_链表  合并_02

通过测试用例来看,当其中一个链表为空时,上面的代码不执行while循环,直接执行下面的if语句,而此时tail为空,所以出现了错误.

修改方法

1.直接判空返回(在声明前加入)

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    if(list1 == NULL)//判空返回
         return list2;//
    if(list2 == NULL)//
         return list1;//

    struct ListNode* head = NULL,*tail = NULL;
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;
    while(cur1 && cur2)
    {
        if(cur1->val<cur2->val)
        {
            if(head == NULL)
            {
                head = tail = cur1;//为空直接赋值
            }
            else
            {
                 tail->next = cur1;
                 tail = tail->next;


            }
           
            cur1 = cur1->next;
            
        }
        else
        {
            if(head == NULL)
            {
                head = tail = cur2;
            }
            else
            {
                 tail->next = cur2;
                 tail = tail->next;


            }
           
            cur2 = cur2->next;


        }
    
    }
    if(cur2)
    {
        tail->next = cur2;
    }
    if(cur1)
    {
        tail->next = cur1;
    }
    return head;



}

2.引用带哨兵卫的头结点(可以减少代码量,不存放有效数据)

哨兵位的头结点不存放有效数据,但是一直存在,所以不需要判空,需要开辟和释放空间

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */



struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;
     struct ListNode* guard = NULL,*tail = NULL;
    guard = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    tail->next = NULL;
    while(cur1 && cur2)
    {
        if(cur1->val<cur2->val)
        {
        //     if(head == NULL)
        //     {
                // head = tail = cur1;//为空直接赋值
            // }
            // else
            // {
                 tail->next = cur1;
                 tail = tail->next;
            // }          
                 cur1 = cur1->next;           
        }
        else
        {
            // if(head == NULL)
            // {
                // head = tail = cur2;
            // }
            // else
            // {
                 tail->next = cur2;
                 tail = tail->next;
            // }         
                cur2 = cur2->next;
        }
    
    }
    if(cur2)
    {
        tail->next = cur2;
    }
    if(cur1)
    {
        tail->next = cur1;
    }

    struct ListNode* head = guard->next;//存放哨兵位头结点的下一位,方便返回
    free(guard);
    return head;
}


【感谢龙石为本站提供数据api平台http://www.longshidata.com/pages/exchange.html】
上一篇:带专人学c(四种常量)
下一篇:没有了
网友评论