zoukankan      html  css  js  c++  java
  • 148. Sort List

    问题描述:

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

    Example 1:

    Input: 4->2->1->3
    Output: 1->2->3->4
    

    Example 2:

    Input: -1->5->3->4->0
    Output: -1->0->3->4->5

    解题思路:

    说到O(nlogn)那就必然想到快速排序和归并排序和堆排序

    参考大佬整理的方法

    代码:

    归并排序

    这里用快慢指针找到中点并且然后将链表打碎成2个链表

    然后对两个链表分别排序后,再调用归并。

    /**
     * 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 || !head->next)
                return head;
            ListNode *slow = head, *fast = head, *pre = head;
            while(fast && fast->next){
                pre = slow;
                slow = slow->next;
                fast = fast->next->next;
            }
            pre->next = NULL;
            return merge(sortList(head), sortList(slow));
        }
        ListNode* merge(ListNode* l1, ListNode* l2){
            if(!l1) return l2;
            if(!l2) return l1;
            if(l1->val < l2->val) {
                l1->next = merge(l1->next, l2);
                return l1;
            } else {
                l2->next = merge(l1, l2->next);
                return l2;
            }
        }
    };

    堆排序:

    注意比较器的重写!!

    struct cmp{
            bool operator() (const ListNode* l1, const ListNode* l2){
                return l1->val > l2->val;
            }

    参考链接:priority_queue & 结构体||类 & 自定义比较函数cmp

    以及使用:

    priority_queue<ListNode*, vector<ListNode*>, cmp> q;
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
        struct cmp{
            bool operator() (const ListNode* l1, const ListNode* l2){
                return l1->val > l2->val;
            }
        };
    public:
        ListNode* sortList(ListNode* head) {
            if(!head || !head->next)
                return head;
            priority_queue<ListNode*, vector<ListNode*>, cmp> q;
            ListNode* ret;
            ListNode *p = head;
            while(p){
                ListNode* temp = p->next;
                p->next = NULL;
                q.push(p);
                p = temp;
            }
            ret = q.top();
            q.pop();
            ListNode *pre = ret;
            while(!q.empty()){
                ListNode *cur = q.top();
                q.pop();
                pre->next = cur;
                pre = cur;
            }
            return ret;
        }
    };
  • 相关阅读:
    z-index
    点击按钮跳转带指定的位置
    导航-角
    弹出框
    控制叠加风格
    Python学习三
    玩转HTTP
    http-关于application/x-www-form-urlencoded等字符编码的解释说明
    Python学习二
    git merge 和 git rebase
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9165332.html
Copyright © 2011-2022 走看看