Leetcode 148. Sort List

题目描述:

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

我们知道对一个数组进行排序的时候, 时间复杂度取为O(nlogn)的一般就三种, 快排, 归并, 堆排序ps:这里盗用各种排序算法时间复杂度和空间复杂度表 博客中的一张图片, 描述的是以数组为基础的排序算法的时间和空间复杂度,以及稳定性表示情况。

这里写图片描述

于是, 我们本能的想到使用快速排序的思想。
我们首先设定一个privot变量, 然后在遍历链表的过程中, 如果得到的数据比privot大就不动他, 而如果比他小,我们就把他放到链表的起始位置处去, 这个和算法导论中介绍的快排的思想非常相似。
然后, 根据privot所在的位置, 将链表分割成了两个待排序的子问题, 至此问题得到解决。

代码如下:

/**
 * 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) {
        return _sortList(head, nullptr);
    }

private:
    ListNode * _sortList(ListNode * start, ListNode * stop){
        if (start == stop)
            return start;

        ListNode myhead(0);
        ListNode * pre = &myhead;
        ListNode * ppre = pre;
        myhead.next = start;

        int privot = start->val;
        ListNode * pprivot = start;

        while (start != stop){
            if (start->val >= privot){
                start = start->next;
                ppre = ppre->next;
            }               
            else{
                // 删去该节点
                ListNode * pcur = start;
                ppre->next = pcur->next;
                pcur->next = nullptr;
                start = ppre->next;

                // 头插
                pcur->next = pre->next;
                pre->next = pcur;
            }
        }

        ListNode * res = pre->next;
        ListNode * head1 = _sortList(pre->next, pprivot);
        ListNode * head2 = _sortList(pprivot->next, stop);

        pprivot->next = head2;

        return head1;
    }
};

满心欢喜以为通过了, 结果, 提交的时候出现了TLE, 我们知道快排最差情况下的时间复杂度会升到O(n^2), 这里显然遇到比较差的情况了。
为了解决这个问题, 算法导论上一般是采用随机快速排序的方案。然而, 这里我们不太希望将这个问题复杂化, 于是, 我们想到了在冒泡排序的优化算法中, 采用了一个flag标记本轮扫描是否有数据交换, 如果没有直接结束。那么, 我们这里其实也可以借助这个思想, 在链表遍历的过程中, 如果数据已经是排序过的状态了, 那么我们就不在需要对子序列递归调用了!!!于是, 我们得到了我们的AC代码:

/**
 * 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) {
        return _sortList(head, nullptr);
    }

private:
    ListNode * _sortList(ListNode * start, ListNode * stop){
        if (start == stop)
            return start;

        ListNode myhead(0);
        ListNode * pre = &myhead;
        ListNode * ppre = pre;
        myhead.next = start;

        int privot = start->val;
        ListNode * pprivot = start;

        bool NeedChanged = false;
        int incre = privot;
        while (start != stop){
            if (start->val >= privot){      
                // 判断链表是不是递增的
                if (!NeedChanged && start->val >= incre){
                    incre = start->val;
                }
                else{
                    NeedChanged = true;
                }

                start = start->next;
                ppre = ppre->next;
            }               
            else{
                NeedChanged = true;

                // 删去该节点
                ListNode * pcur = start;
                ppre->next = pcur->next;
                pcur->next = nullptr;
                start = ppre->next;

                // 头插
                pcur->next = pre->next;
                pre->next = pcur;
            }
        }

        if (NeedChanged){
            ListNode * res = pre->next;
            ListNode * head1 = _sortList(pre->next, pprivot);
            ListNode * head2 = _sortList(pprivot->next, stop);
            pprivot->next = head2;

            return head1;
        }
        else{
            // 如果数据是递增的就不需要在处理了
            return myhead.next;
        }

    }
};

提交完毕后, 我们看到leetcode上更多的一种方案是采用归并排序, 因为数组不容易移动, 所以空间复杂度为O(n), 但是, 链表容易移动, 空间复杂度可以降到O(1).

下面给出大神们的代码:

class Solution {
public:
    ListNode* merge( ListNode* head1 , ListNode * head2){
        ListNode* d = new ListNode (0);            // dummy node
        ListNode* e = d;
        while(head1||head2){
            if(head1 && (!head2 || head1->val <= head2 -> val) ){
                e=e->next= head1 ; 
                head1 = head1 -> next;
            }
            if(head2 && (!head1 || head2->val < head1 -> val) ){
                e=e->next= head2 ; 
                head2 = head2 -> next;
            }
        }
        e->next = NULL;
        return d->next;
    }
   ListNode* sortList(ListNode* head) {
        if(!head || !head->next) return head;
        ListNode* slow = head;
        ListNode* fast =head->next;
         while(fast && fast->next){         // to find middle node     
            fast= fast->next->next;
            slow = slow->next;
        }
        ListNode* headb = slow->next;     // headb is start of 2nd half of list
        slow->next = NULL;                 
        return merge(sortList(head) , sortList(headb));
        }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值