LeetCode 21.合并两个有序链表

文章目录


题目链接 👉 LeetCode 21.合并两个有序链表👈

💡题目分析

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



💡解题思路

🚩思路1: 归并排序思想(不使用带哨兵卫的头节点)

取小的进行尾插

👇图解👇

🔔接口源码:

c 复制代码
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
	//考虑list1和list2其中一个为空的情况
    if (list1 == NULL)
    {
        return list2;
    }
    if (list2 == NULL)
    {
        return list1;
    }

    struct ListNode* head = NULL, * tail = NULL;
    
    //当list1和list2任意一个为空循环就结束
    while (list1 && list2)
    {
        if (list1->val < list2->val)
        {
            if (tail == NULL)
            {
                head = tail = list1;
            }
            else
            {
                tail->next = list1;
                tail = tail->next;
            }

            list1 = list1->next;
        }
        else
        {
            if (tail == NULL)
            {
                head = tail = list2;
            }
            else
            {
                tail->next = list2;
                tail = tail->next;
            }

            list2 = list2->next;
        }
        
		//如果list1没空则把list1后面剩下的数据直接链接到tail->next的后面
        if (list1)
        {
            tail->next = list1;
        }
        //如果list2没空则把list2后面剩下的数据直接链接到tail->next的后面
        if (list2)
        {
            tail->next = list2;
        }
    }

    return head;
}

💡解题思路

🚩思路2: 归并排序思想(使用带哨兵卫的头节点)

取小的进行尾插

👇图解 👇

🔔接口源码:

c 复制代码
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
	//考虑list1和list2其中一个为空的情况
    if (list1 == NULL)
    {
        return list2;
    }
    if (list2 == NULL)
    {
        return list1;
    }

    struct ListNode* head = NULL, * tail = NULL;
    //带哨兵卫的头节点,这个头节点不存储有效数据
    head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
	
	//当list1和list2任意一个为空循环就结束
    while (list1 && list2)
    {
        if (list1->val < list2->val)
        {
            tail->next = list1;
            tail = tail->next;
            list1 = list1->next;
        }
        else
        {
            tail->next = list2;
            tail = tail->next;
            list2 = list2->next;
        }
    }
	
	//如果list1没空则把list1后面剩下的数据直接链接到tail->next的后面
    if (list1)
    {
        tail->next = list1;
    }
    //如果list2没空则把list2后面剩下的数据直接链接到tail->next的后面
    if (list2)
    {
        tail->next = list2;
    }
	
	//在前面malloc的空间需要释放,释放前先保存head->next的地址
    struct ListNode* del = head;
    head = head->next;
    free(del);

    return head;
}


🥰希望烙铁们能够理解欧!

总结🥰
以上就是本题讲解的全部内容啦🥳🥳🥳🥳
本文章所在【C/C++刷题系列】专栏,感兴趣的烙铁可以订阅本专栏哦🥳🥳🥳
前途很远,也很暗,但是不要怕,不怕的人面前才有路。💕💕💕
小的会继续学习,继续努力带来更好的作品😊😊😊
创作写文不易,还多请各位大佬uu们多多支持哦🥰🥰🥰

相关推荐
程序员清风17 分钟前
算法与数据结构入门:复杂度、数组与链表
数据结构·算法·链表
无定义_27 分钟前
Codeforces Round 1124 (Div. 2)
算法
山下梅子酒22529 分钟前
洛谷-入门-B2079
c语言
Mr_Macallon2 小时前
C++回顾(基础)(01)
c语言·开发语言·c++·学习
垆边人似月.2 小时前
任务混部(200 分档)
算法
言乐62 小时前
Python区分广度优先深度优先宽度优先的区别
python·算法·深度优先·广度优先·宽度优先
Rnan-prince2 小时前
LeetCode 股票买卖系列:一个动态规划状态机模板通吃四题
leetcode·动态规划
如意猴2 小时前
【C++】007 C/C++ 内存管理机制、malloc与new的区别及模板初阶
java·c++·算法
朝朝辞暮i2 小时前
C++ 第 32 课:main()、rclcpp::init()、spin()、shutdown()
开发语言·c++·算法
圣保罗的大教堂3 小时前
leetcode 3756. 连接非零数字并乘以其数字和 II 中等
leetcode