leetcode 148. 排序链表(非递归归并排序/以及不需要快慢指针的归并)

在这里插入图片描述

class Solution {
public:
    int getlen(ListNode *head){
        int cnt=0;
        while(head) ++cnt,head=head->next;
        return cnt; 
    }
    ListNode* mov(ListNode*& x,int len,int flag){//x有可能为NULL
        //if(!x) return NULL;
        ListNode *pre=NULL;
        while(len--&&x) pre=x,x=x->next;
        if(pre&&flag) pre->next=NULL;
        return x;
    }
    void cat(ListNode* x,ListNode *y){
        while(x->next) x=x->next;
        x->next=y;
    }
    ListNode* merge(ListNode* x,ListNode *y){//x一定不为NULL,y可能为NULL(反正x一定比y长)
        if(!y) return x;
        ListNode *head=new ListNode(),*now=head;
        while(x&&y){
            if(x->val<y->val) now=now->next=x,x=x->next;
            else now=now->next=y,y=y->next; 
        }
        while(x) now=now->next=x,x=x->next;
        while(y) now=now->next=y,y=y->next;
        return head->next;
    }
    ListNode* sort_dep(ListNode *x,int len){//x后面可能不足len个,更不足2*len个
        if(!x) return NULL;
        ListNode *tmpx=x;
        ListNode *mid=mov(x,len,1),*y=mov(mid,len,1);//y可能为NULL,mid也可能为NULL
        ListNode *res=merge(tmpx,x);
        cat(res,y);
        return res;
    }
    ListNode* sortList(ListNode* head) {
        if(!head) return NULL;
        ListNode *ans=new ListNode(),*p=ans;
        ans->next=head;
        for(int len=1,n=getlen(head);len<n;len<<=1){
            while(p){
                p->next=sort_dep(p->next,len);//将[p-next,p->next+len-1],[p->nex+len,p->next+len+len-1];
                mov(p,2*len,0);//p就为NULL了,
            }
            p=ans;
        }
        return ans->next;
    }
};
class Solution {
public:
    int getlen(ListNode *x){
        int cnt=0;
        while(x) x=x->next,++cnt;
        return cnt;
    }
    ListNode* merge(ListNode* x,ListNode *y){
        if(!y) return x;
        if(!x) return y;
        ListNode *head=new ListNode(),*now=head;
        while(x&&y){
            if(x->val<y->val) now=now->next=x,x=x->next;
            else now=now->next=y,y=y->next;
        }
        while(x) now=now->next=x,x=x->next;
        while(y) now=now->next=y,y=y->next;
        return head->next;
    }
    //sort之后[l,r]排好序 head是排好序链表头节点 且r->next置为空,且返回原来的r->next
    ListNode* sort(ListNode*& head,int l,int r){
        if(l==r) {
            ListNode *ans=head->next;//下一节点
            head->next=NULL;//断开
            return ans;//返回下一个需要排序的节点
        }
        int mid=(l+r)>>1;
        ListNode *rs=sort(head,l,mid);//head已经是排好序的左二子头节点 rs是排完左儿子返回的节点,即右区间第一个节点
        ListNode *nex=sort(rs,mid+1,r);//rs已经是排好序的右儿子头节点
        head=merge(head,rs);
        return nex;
    }
    ListNode* sortList(ListNode* head) {
        if(!head) return head;
        sort(head,0,getlen(head)-1);
        return head;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值