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;
    }
};
相关推荐
唐梓航-求职中11 小时前
编程大师-技术-算法-leetcode-1472. 设计浏览器历史记录
算法·leetcode
_OP_CHEN11 小时前
【算法基础篇】(五十八)线性代数之高斯消元法从原理到实战:手撕模板 + 洛谷真题全解
线性代数·算法·蓝桥杯·c/c++·线性方程组·acm/icpc·高斯消元法
YGGP11 小时前
【Golang】LeetCode 1. 两数之和
leetcode
唐梓航-求职中11 小时前
编程大师-技术-算法-leetcode-355. 设计推特
算法·leetcode·面试
少许极端11 小时前
算法奇妙屋(二十八)-递归、回溯与剪枝的综合问题 1
java·算法·深度优先·剪枝·回溯·递归
仰泳的熊猫11 小时前
题目1453:蓝桥杯历届试题-翻硬币
数据结构·c++·算法·蓝桥杯
唐梓航-求职中11 小时前
技术-算法-leetcode-1606. 找到处理最多请求的服务器(易懂版)
服务器·算法·leetcode
啊阿狸不会拉杆11 小时前
《机器学习导论》第 10 章-线性判别式
人工智能·python·算法·机器学习·numpy·lda·线性判别式
会叫的恐龙11 小时前
C++ 核心知识点汇总(第11日)(排序算法)
c++·算法·排序算法
twilight_46911 小时前
机器学习与模式识别——线性回归算法
算法·机器学习·线性回归