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们多多支持哦🥰🥰🥰

相关推荐
踩坑记录41 分钟前
leetcode hot100 寻找两个正序数组的中位数 hard 二分查找 双指针
leetcode
旖-旎43 分钟前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历
谭欣辰1 小时前
C++快速幂完整实战讲解
算法·决策树·机器学习
Mr_pyx1 小时前
【LeetHOT100】随机链表的复制——Java多解法详解
算法·深度优先
AIFarmer1 小时前
【无标题】
开发语言·c++·算法
AGV算法笔记1 小时前
CVPR 2025 最新感知算法解读:GaussianLSS 如何用 Gaussian Splatting 重构 BEV 表示?
算法·重构·自动驾驶·3d视觉·感知算法·多视角视觉
勤劳的进取家2 小时前
数据链路层基础
网络·学习·算法
dgaf3 小时前
DX12 快速教程(17) —— 立体图标与合并渲染
c语言·c++·3d·图形渲染·d3d12
Advancer-3 小时前
第二次蓝桥杯总结(上)
java·算法·职场和发展·蓝桥杯
ん贤3 小时前
加密算法(对称、非对称、哈希、签名...)
算法·哈希算法