合并两个有序链表 - 简单

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

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.

相关推荐
艾莉丝努力练剑1 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
_殊途3 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
秋说7 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen8 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
risc1234569 小时前
BKD 树(Block KD-Tree)Lucene
java·数据结构·lucene
kk_stoper10 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源
秋说10 小时前
【PTA数据结构 | C语言版】字符串插入操作(不限长)
c语言·数据结构·算法
遇见尚硅谷12 小时前
C语言:20250714笔记
c语言·开发语言·数据结构·笔记·算法
恸流失14 小时前
java基础-1 : 运算符
java·开发语言·数据结构
yu20241115 小时前
【【异世界历险之数据结构世界(二叉树)】】
数据结构·算法