zoukankan      html  css  js  c++  java
  • sort-list

    原文地址:https://www.jianshu.com/p/4f3bf22ea9de

    时间限制: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

  • 相关阅读:
    oo第四次作业总结
    oo第三次博客总结
    oo第二次博客总结
    oo第一次博客总结
    leetcode155-最小栈
    leetcode141-环形链表
    leetcode278-第一个错误的版本
    leetcode118-杨辉三角
    LeetCode21-合并两个有序列表
    LeetCode27-移除元素
  • 原文地址:https://www.cnblogs.com/cherrychenlee/p/10831500.html
Copyright © 2011-2022 走看看