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;
    }
};
相关推荐
mit6.8241 小时前
前后缀分解
算法
独自破碎E2 小时前
判断链表是否为回文
数据结构·链表
你好,我叫C小白2 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
寂静山林4 小时前
UVa 10228 A Star not a Tree?
算法
Neverfadeaway4 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
Madison-No75 小时前
【C++】探秘vector的底层实现
java·c++·算法
Swift社区5 小时前
LeetCode 401 - 二进制手表
算法·leetcode·ssh
派大星爱吃猫5 小时前
顺序表算法题(LeetCode)
算法·leetcode·职场和发展
liu****5 小时前
8.list的模拟实现
linux·数据结构·c++·算法·list
地平线开发者6 小时前
征程 6 | 征程 6 工具链如何支持 Matmul/Conv 双 int16 输入量化?
算法·自动驾驶