合并两个有序链表 - 简单

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

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.

相关推荐
散1124 小时前
01数据结构-01背包问题
数据结构
消失的旧时光-19434 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww5 小时前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚6 小时前
[数据结构] 排序
数据结构
_不会dp不改名_8 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
睡不醒的kun8 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌8 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long9 小时前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn9 小时前
Redis7底层数据结构解析
前端·数据结构·bootstrap
我星期八休息9 小时前
深入理解跳表(Skip List):原理、实现与应用
开发语言·数据结构·人工智能·python·算法·list