合并两个有序链表 - 简单

*************

C++

topic: 21. 合并两个有序链表 - 力扣(LeetCode)

*************

Give the topic an inspection.

|----------------------------------------------------------------------------|
| |

Hi, guys, how is your holiday break? I went to 黄山 in the past few days. The mount Huang is really beautiful. 天都峰 is really hard to get the top. As is knwon, 90° is a wall, 85° is a slope. Huangshan do have fantastic views, if you have been there, you will get me. 呈坎的鱼灯 also is fantastic. I heard that yudeng is the properties of the crew 《家业》. Local people there bought the yudeng properties after filming. And then they do the yudeng show in the holidays. You can touch it and dancing with it. There are foreworks at the same time. I really enjoy the architecture there. Beautiful there. But the food 臭鳜鱼 there is not so good, eww.

|----------------------------------------------------------------------------|
| |
| |
| |

ok back to the code topic. you see the struct listNote, it rells you the basic uses of linked list. Let's learn the basic usages of the linked list.

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;                                                  // 节点存储的值
 *     ListNode *next;                                           // 指向下一个节点的指针
 *     ListNode() : val(0), next(nullptr) {}                     // 默认构造函数
 *     ListNode(int x) : val(x), next(nullptr) {}                // 带值参数的构造函数
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}   // 带值和next指针的构造函数
 * };
 */

1、Define the linked list dot strunt

cpp 复制代码
// 定义链表节点结构体
struct ListNode {
    int val;         // 节点存储的值
    ListNode *next;  // 指向下一个节点的指针
    ListNode(int x) : val(x), next(nullptr) {} // 构造函数初始化
};

2、Creat a linked list

cpp 复制代码
// 创建链表 1 -> 2 -> 3
ListNode *head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);

3、Insert a dot in the front of the linked list

cpp 复制代码
// 在头部插入节点 0
ListNode *newNode = new ListNode(0);
newNode->next = head;
head = newNode;

// head -> 0 -> 1 -> 2 -> 3 -> nullptr

4、Insert a dot in the back of the linked list

cpp 复制代码
// 在尾部插入节点 4
ListNode* current = head;
while (current->next != nullptr) {
    current = current->next;
}
current->next = new ListNode(4);

//head -> 0 -> 1 -> 2 -> 3 -> 4 -> nullptr

5、delete the dot

cpp 复制代码
// 删除值为2的节点
current = head;
ListNode *prev = nullptr;
while (current != nullptr) {
    if (current->val == 2) {
        if (prev == nullptr) { // 如果删除的是头节点
            head = current->next;
        } else {
            prev->next = current->next;
        }
        delete current;
        break;
    }
    prev = current;
    current = current->next;
}

//head -> 0 -> 1 -> 3 -> 4 -> nullptr

and back to the topic. Use a dummy node to

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;        // 节点存储的值
 *     ListNode *next; // 指向下一个节点的指针
 *     ListNode() : val(0), next(nullptr) {}                     // 默认构造函数
 *     ListNode(int x) : val(x), next(nullptr) {}                // 带值参数的构造函数
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}   // 带值和next指针的构造函数
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        // 创建一个哑节点(dummy node),它的next指针将指向合并后的链表头
        // 使用哑节点可以避免处理空链表的特殊情况,简化代码逻辑
        ListNode dummy(0);
        
        // tail指针用于跟踪新链表的最后一个节点,初始指向哑节点
        ListNode *tail = &dummy;
        
        // 循环比较两个链表的当前节点,直到其中一个链表为空
        while (list1 != nullptr && list2 != nullptr) 
        {
            // 比较两个链表当前节点的值
            if (list1->val <= list2->val) 
            {
                // 如果list1的节点值较小,将其加入新链表
                tail->next = list1;
                // 移动list1指针到下一个节点
                list1 = list1->next;
            } 
            else 
            {
                // 如果list2的节点值较小,将其加入新链表
                tail->next = list2;
                // 移动list2指针到下一个节点
                list2 = list2->next;
            }
            // 移动tail指针到新链表的最后一个节点
            tail = tail->next;
        }
        
        // 循环结束后,至少有一个链表已经遍历完
        // 将剩余的非空链表直接连接到新链表的末尾
        // 这里使用三元运算符判断哪个链表还有剩余节点
        tail->next = (list1 != nullptr) ? list1 : list2;
        
        // 返回合并后的链表头节点(哑节点的next指针指向的节点)
        return dummy.next;
    }
};

I donot like linked list because i am not familiar with it. do it again tomorrow.

and have a good day.

相关推荐
好好研究2 小时前
学习栈和队列的插入和删除操作
数据结构·学习
挺菜的4 小时前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
2401_858286115 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
双叶8366 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
学不动CV了8 小时前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
百年孤独_10 小时前
LeetCode 算法题解:链表与二叉树相关问题 打打卡
算法·leetcode·链表
算法_小学生11 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
Tanecious.13 小时前
LeetCode 876. 链表的中间结点
算法·leetcode·链表
Wo3Shi4七13 小时前
哈希冲突
数据结构·算法·go
V我五十买鸡腿14 小时前
顺序栈和链式栈
c语言·数据结构·笔记·算法