Leetcode 148. 排序链表 归并排序

原题链接:Leetcode 148. 排序链表


方法一:自顶向下归并排序

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) {}
 * };
 */
class Solution {
public:
    ListNode* merge2Lists(ListNode* head1,ListNode* head2){
        if(!head1) return head2;
        if(!head2) return head1;
        if(head1-> val <=  head2->val){
            head1->next = merge2Lists(head1->next,head2);
            return head1;
        }
        else {
            head2->next = merge2Lists(head1,head2->next);
            return head2;
        }
    }
    ListNode* sortList(ListNode* head) {
        if(head==nullptr || head->next==nullptr) return head;
        // 快慢指针找到中间节点
        ListNode * slow = head;
        ListNode * fast = head;
        ListNode * pre = slow;
        while(fast && fast->next){
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        // 找到中间节点 slow,断开slow的前一个节点pre和slow的关系,将链表拆分成两个子链表
        pre ->next = nullptr;
        // 对两个子链表分别排序
        head = sortList(head);
        slow = sortList(slow);
        // 合并两个有序链表
        return merge2Lists(head,slow);
    }
};

方法二:自底向上归并排序

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) {}
 * };
 */
class Solution {
public:
    ListNode* merge2Lists(ListNode* head1,ListNode* head2){
        if(!head1) return head2;
        if(!head2) return head1;
        if(head1-> val <=  head2->val){
            head1->next = merge2Lists(head1->next,head2);
            return head1;
        }
        else {
            head2->next = merge2Lists(head1,head2->next);
            return head2;
        }
    }
    ListNode* sortList(ListNode* head) {
        if(head==nullptr || head->next==nullptr) return head;
        int n=0;
        ListNode* cur = head;
        while(cur){
            cur=cur->next;
            n++;
        }
        ListNode* root = new ListNode (0,head);
        for(int len=1;len<n;len*=2){
            ListNode* pre = root;
            cur = root->next;
            while(cur!=nullptr){
                ListNode* head1 = cur;
                for(int i=1;i<len && cur->next!=nullptr;i++){
                    cur = cur->next;
                }
                ListNode* head2 = cur->next;
                cur->next = nullptr;
                cur= head2;
                for(int i=1;i<len && cur!=nullptr && cur->next!=nullptr ;i++){
                    cur = cur->next;
                }
                ListNode* nxt = nullptr;
                if(cur!=nullptr){
                    nxt = cur->next;
                    cur->next = nullptr;
                }
                ListNode* merged = merge2Lists(head1,head2);
                pre->next=merged;
                while(pre->next!=nullptr){
                    pre = pre->next;
                }
                cur = nxt;
            }
        }
        return root->next;
    }
};
相关推荐
2301_822703209 小时前
光影进度条:鸿蒙Flutter实现动态光影效果的进度条
算法·flutter·华为·信息可视化·开源·harmonyos
人道领域9 小时前
【LeetCode刷题日记】383 赎金信
算法·leetcode·职场和发展
炽烈小老头9 小时前
【每天学习一点算法 2026/04/11】Pow(x, n)
学习·算法
旖-旎9 小时前
哈希表(存在重复元素)(3)
数据结构·c++·学习·算法·leetcode·散列表
明月醉窗台9 小时前
[jetson] AGX Xavier 安装Ubuntu18.04及jetpack4.5
人工智能·算法·nvidia·cuda·jetson
计算机安禾9 小时前
【数据结构与算法】第39篇:图论(三):最小生成树——Prim算法与Kruskal算法
开发语言·数据结构·c++·算法·排序算法·图论·visual studio code
weixin_513449969 小时前
walk_these_ways项目学习记录第九篇(通过行为多样性 (MoB) 实现地形泛化)--学习算法
学习·算法·机器学习
fish_xk9 小时前
c++内存管理
开发语言·c++·算法
Tisfy9 小时前
LeetCode 3740.三个相等元素之间的最小距离 I:今日先暴力,“明日“再哈希
算法·leetcode·哈希算法·题解·模拟·遍历·暴力
汀、人工智能9 小时前
[特殊字符] 第77课:最长递增子序列
数据结构·算法·数据库架构·图论·bfs·最长递增子序列