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

相关推荐
许愿与你永世安宁20 分钟前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子23 分钟前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
ruanjiananquan9926 分钟前
c,c++语言的栈内存、堆内存及任意读写内存
java·c语言·c++
满分观察网友z1 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
持梦远方1 小时前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++
YuTaoShao1 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx1 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背2 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z2 小时前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法
杰克尼3 小时前
1. 两数之和 (leetcode)
数据结构·算法·leetcode