sort-list

作者: cherryleechen | 来源:发表于2019-05-08 14:06 被阅读0次

    时间限制:1秒 空间限制:32768K

    题目描述

    Sort a linked list in O(nlogn) time using constant space complexity.

    我的代码

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode *sortList(ListNode *head) {
            if(head==nullptr || head->next==nullptr)
                return head;
            ListNode* fast=head->next;
            ListNode* slow=head;
            while(fast&&fast->next){
                fast=fast->next->next;
                slow=slow->next;
            }
            ListNode* left=sortList(slow->next);
            slow->next=nullptr;
            ListNode* right=sortList(head);
            return merge(left,right);
        }
        ListNode* merge(ListNode* left,ListNode* right){
            ListNode tmp(0);
            ListNode* p=&tmp;
            while(left&&right){
                if(left->val<right->val){
                    p->next=left;
                    left=left->next;
                }
                else{
                    p->next=right;
                    right=right->next;
                }
                p=p->next;
            }
            if(left)
                p->next=left;
            if(right)
                p->next=right;
            return tmp.next;
        }//时复:O(nlogn),空复:O(logn)
    };
    

    运行时间:13ms
    占用内存:1232k

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode *sortList(ListNode *head) {
            if(head==nullptr || head->next==nullptr)
                return head;
            int len=lenofList(head);
            ListNode tmp(0);
            tmp.next=head;
            for(int i=1;i<len;i<<=1){
                ListNode* cur=tmp.next;
                ListNode* tail=&tmp;
                while(cur){
                    ListNode* left=cur;
                    ListNode* right=cut(left,i);
                    cur=cut(right,i);
                    tail->next=merge(left,right);
                    while(tail->next)
                        tail=tail->next;
                }
            }
            return tmp.next;
        }
        int lenofList(ListNode* p){
            int len=0;
            while(p){
                len++;
                p=p->next;
            }
            return len;
        }
        ListNode* cut(ListNode* p, int len){
            while(p&&(--len)){
                p=p->next;
            }
            if(!p)
                return nullptr;
            ListNode* next=p->next;
            p->next=nullptr;
            return next;
        }
        ListNode* merge(ListNode* left,ListNode* right){
            ListNode tmp(0);
            ListNode* p=&tmp;
            while(left&&right){
                if(left->val<right->val){
                    p->next=left;
                    left=left->next;
                }
                else{
                    p->next=right;
                    right=right->next;
                }
                p=p->next;
            }
            if(left)
                p->next=left;
            if(right)
                p->next=right;
            return tmp.next;
        }//时复:O(nlogn),空复:O(1)
    };
    

    运行时间:12ms
    占用内存:1004k

    相关文章

      网友评论

        本文标题:sort-list

        本文链接:https://www.haomeiwen.com/subject/sxymoqtx.html