合并两个有序链表 - 简单

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

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.

相关推荐
阳洞洞29 分钟前
leetcode 141. Linked List Cycle
数据结构·leetcode·链表·双指针
似水এ᭄往昔2 小时前
【数据结构】——单链表练习(1)
数据结构
whoarethenext3 小时前
数据结构堆的c/c++的实现
c语言·数据结构·c++·
Mi Manchi264 小时前
力扣热题100之反转链表
算法·leetcode·链表
n33(NK)4 小时前
【算法基础】选择排序算法 - JAVA
数据结构·算法·排序算法
CS创新实验室4 小时前
408考研逐题详解:2009年第6题
数据结构·考研·算法·408·真题·计算机考研·408计算机
阳洞洞6 小时前
leetcode 142. Linked List Cycle II
数据结构·leetcode·链表·双指针
apcipot_rain6 小时前
【计算机网络 第8版】谢希仁编著 第四章网络层 地址类题型总结
数据结构·算法
NON-JUDGMENTAL7 小时前
第2章 算法分析基础
java·数据结构·算法
wen__xvn8 小时前
每日一题洛谷P1025 [NOIP 2001 提高组] 数的划分c++
数据结构·c++·算法