随笔 - 112  文章 - 0  评论 - 0  阅读 - 1488

排序链表(归并排序)

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

 

示例 1:

输入:head = [4,2,1,3]
输出:[1,2,3,4]

示例 2:

输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]

示例 3:

输入:head = []
输出:[]

 

方法一:归并排序

复制代码
/**
 * 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* sortList(ListNode* head) {
        return sortList(head,nullptr);
    }
    ListNode* sortList(ListNode* head,ListNode* tail) {
        if(head==nullptr) return head;
        //如果只有一个节点
        if(head->next==tail){
            head->next = nullptr;
            return head;
        }
        //找到链表的中点
        ListNode *fast=head,*slow = head;
        while(fast!=tail){
            slow = slow->next;
            fast = fast->next;
            if(fast!=tail){
                fast = fast->next;
            }
        }
        ListNode* mid = slow;
        //归并排序
        return merge(sortList(head,mid),sortList(mid,tail));
    }
    ListNode* merge(ListNode* list1,ListNode* list2){
        ListNode* preHead = new ListNode(-1);
        ListNode* pre = preHead;
        ListNode* head1 = list1;
        ListNode* head2 = list2;
        while(head1&&head2){
            if(head1->val<=head2->val){
                pre->next = head1;
                pre = head1;
                head1=head1->next;
            }else{
                pre->next = head2;
                pre = head2;
                head2=head2->next;
            }
        }
        if(head1){
            pre->next = head1;
        }else{
            pre->next = head2;
        }
        return preHead ->next;
    }
};
复制代码

 

 

方法二:使用set集合默认按照key进行排序

复制代码
/**
 * 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* sortList(ListNode* head) {
        if(head==nullptr) return head;
        set<pair<int,ListNode*>> st;
        ListNode *node = head;
        while(node){
            st.insert({node->val,node});
            node = node->next;
        }
        for(auto it = st.begin();it!=st.end();it++){
            auto temp = next(it);
            if(temp==st.end()){
                it->second->next = nullptr;
            }else{
                it->second->next = temp->second;
            }
        }
        return st.begin()->second;
    }
};
复制代码

 

posted on   _月生  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示