合并两个有序链表 - 简单

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

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 小时前
《算法导论》第 2 章 - 算法基础
数据结构·c++·算法·排序算法
啊阿狸不会拉杆2 小时前
《算法导论》第 4 章 - 分治策略
开发语言·数据结构·c++·算法·排序算法
设计师小聂!3 小时前
力扣热题100-------169.多数元素
java·数据结构·算法·leetcode·多数元素
艾莉丝努力练剑4 小时前
【数据结构与算法】顺序表和链表、栈和队列、二叉树、排序等数据结构的完整代码收录
c语言·数据结构·学习·链表
啊阿狸不会拉杆4 小时前
《算法导论》第 3 章 - 函数的增长
开发语言·数据结构·c++·算法
柠檬味的薄荷心5 小时前
【数据结构】双链表
数据结构
Aczone285 小时前
数据结构(三)双向链表
java·数据结构·链表
无敌的大魔王5 小时前
数据结构 实现单链表
数据结构
MSXmiao7 小时前
2048小游戏
数据结构·c++·算法
钮钴禄·爱因斯晨7 小时前
数据结构 | 树的秘密
c语言·开发语言·数据结构