码迷,mamicode.com
首页 > 其他好文 > 详细

刷题148. Sort List

时间:2020-03-21 09:21:44      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:归并排序   ges   递归   性能   函数   复杂度   runtime   非递归   memory   

一、题目说明

题目148. Sort List,对链表进行排序,时间复杂度要求是O(nlog(n)),空间复杂度要求是常量。难度是Medium!

二、我的解答

根据要求,唯一符合标准的是归并排序。

class Solution{
    public:
        ListNode* sortList(ListNode* head){
            if(head==NULL || head->next==NULL) return head;
            return mergeSort(head);
        }
        //归并排序 
        ListNode* mergeSort(ListNode* node){
            if(node==NULL || node->next==NULL) return node;
            ListNode* fast= node,*slow = node;
            ListNode* breakNode = node;//breakNode 指向l1的最后一个元素 
            //也可以采用先遍历一遍,统计链表节点的数量,然后归并排序
            //找到链表中间 
            while(fast && fast->next){
                fast = fast->next->next;
                breakNode = slow;
                slow = slow->next;
            }
            breakNode->next = NULL;
            ListNode* l1 = mergeSort(node);
            ListNode* l2 = mergeSort(slow);
            
            return merge(l1,l2);
        } 
        
        //合并两个链表 recursive
        ListNode* merge(ListNode* l1,ListNode* l2){
            if(l1 == NULL){
                return l2;
            }
            if(l2 == NULL){
                return l1;
            }
            if(l1->val < l2->val){
                l1->next = merge(l1->next,l2);
                return l1;
            }else{
                l2->next = merge(l2->next,l1);
                return l2;
            }
        } 
};

性能如下:

Runtime: 60 ms, faster than 41.32% of C++ online submissions for Sort List.
Memory Usage: 16.2 MB, less than 15.00% of C++ online submissions for Sort List.

三、优化措施

将merge函数,修改为非递归版本:

class Solution{
    public:
        ListNode* sortList(ListNode* head){
            if(head==NULL || head->next==NULL) return head;
            return mergeSort(head);
        }
        //归并排序 
        ListNode* mergeSort(ListNode* node){
            if(node==NULL || node->next==NULL) return node;
            ListNode* fast= node,*slow = node;
            ListNode* breakNode = node;//breakNode 指向l1的最后一个元素 
            //找到链表中间 
            while(fast && fast->next){
                fast = fast->next->next;
                breakNode = slow;
                slow = slow->next;
            }
            breakNode->next = NULL;
            ListNode* l1 = mergeSort(node);
            ListNode* l2 = mergeSort(slow);
            
            return merge(l1,l2);
        } 
        //合并两个链表
        ListNode* merge(ListNode* l1,ListNode* l2){
            if(l1 == NULL) return l2;
            if(l2 == NULL) return l1;
            ListNode dummy(0);
            ListNode* p = &dummy;
            while(l1!=NULL && l2!=NULL){
                if(l1->val < l2->val){
                    p->next = l1;
                    l1 = l1->next;  
                }else{
                    p->next = l2;
                    l2 = l2->next;
                }
                p = p->next;
            }
            if(l1 !=NULL){
                p->next = l1;
            }
            if(l2 !=NULL){
                p->next = l2;
            }
            return dummy.next;
        }
};

性能如下:

Runtime: 52 ms, faster than 67.44% of C++ online submissions for Sort List.
Memory Usage: 15 MB, less than 15.00% of C++ online submissions for Sort List.

刷题148. Sort List

标签:归并排序   ges   递归   性能   函数   复杂度   runtime   非递归   memory   

原文地址:https://www.cnblogs.com/siweihz/p/12274559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!