leetcode148.排序链表

对链表进行归并排序

原题目链接 排序链表
本题目要求时间复杂度为O(nlogn)因此不难想到使用归并排序,而对与链表的归并排序来说,比较核心的算法就是进行断链和重组
采用的算法如下:

    //断链表
    ListNode *disconnent(ListNode *p,int num){
        for(int i=0;i<num-1 && p;i++){
            p=p->next;
        }
        ListNode *res=nullptr;
        if(p){
            res=p->next;
            p->next=nullptr;
        }
        return res;
    }
//归并链表
    ListNode *Merge(ListNode *p,ListNode *q){
        ListNode *_head=new ListNode;
        ListNode *cur=_head;
        while(p && q){
            if(p->val<=q->val){
                cur->next=p;
                p=p->next;
            }
            else{
                cur->next=q;
                q=q->next;
            }
            cur=cur->next;
        }
        if(p) cur->next=p;
        else cur->next=q;
        return _head->next;
    }

整个题目算法如下:

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        ListNode *_head=new ListNode;
        _head->next=head;
        int count=length(head);
        ListNode *pre,*cur1,*cur2,*next;
        for(int i=1;i<count;i*=2){
            pre=_head;
            cur1=pre->next;
            next=cur1;
            while(next){
                cur2=disconnent(cur1,i);
                next=disconnent(cur2,i);
                pre->next=Merge(cur1,cur2);
                pre=find_end(pre);
                cur1=next;
            }
        }
        return _head->next;
    }
    //获取链表的长度
    int length(ListNode *head){
        int count=0;
        while(head){
            count++;
            head=head->next;
        }
        return count;
    }
    //断链表
    ListNode *disconnent(ListNode *p,int num){
        for(int i=0;i<num-1 && p;i++){
            p=p->next;
        }
        ListNode *res=nullptr;
        if(p){
            res=p->next;
            p->next=nullptr;
        }
        return res;
    }
    //归并链表
    ListNode *Merge(ListNode *p,ListNode *q){
        ListNode *_head=new ListNode;
        ListNode *cur=_head;
        while(p && q){
            if(p->val<=q->val){
                cur->next=p;
                p=p->next;
            }
            else{
                cur->next=q;
                q=q->next;
            }
            cur=cur->next;
        }
        if(p) cur->next=p;
        else cur->next=q;
        return _head->next;
    }
    //查找当前最后节点
    ListNode *find_end(ListNode *p){
        while(p->next){
            p=p->next;
        }
        return p;
    }
    //测试打印输出
    void print(ListNode *head){
        while(head){
            cout<<head->val<<" ";
            head=head->next;
        }
        cout<<endl;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值