148. Sort List (List)

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

class Solution {
public:
    ListNode *sortList(ListNode *head) {
        if(!head || !head->next ) return head;
        return quickSort(head)[0];
    }

    vector< ListNode* > quickSort(ListNode *head){
        vector< ListNode* > ret;
        if(!head->next) {
            ret.push_back(head);
            ret.push_back(head);
            return ret;
        }
        ListNode* headSmaller = NULL;
        ListNode* headGreater = NULL;
        ListNode* current = head->next;
        ListNode* prev = head;
        ListNode* tmp;
        while(current){
            if(current->val > head->val){
                prev->next = current->next;
                if(!headGreater){
                    headGreater = current;
                    headGreater->next = NULL;
                }
                else{
                    current->next  = headGreater->next;
                    headGreater->next = current;
                }
                current = prev->next;
            }
            else if(current->val < head->val){
                prev->next = current->next;
                if(!headSmaller){
                    headSmaller = current;
                    headSmaller->next = NULL;
                }
                else{
                    current->next  = headSmaller->next;
                    headSmaller->next = current;
                }
                current = prev->next;
            }
            else {
                prev = current;
                current = current->next;
            }
        }

        vector< ListNode* > retGreater;
        vector< ListNode* > retSmaller;
        if(headSmaller) {
            retSmaller = quickSort(headSmaller);
            retSmaller[1]->next = head;
            ret.push_back(retSmaller[0]);
        }
        else ret.push_back(head);
        if(headGreater){
           retGreater = quickSort(headGreater);
           prev->next = retGreater[0];
           ret.push_back(retGreater[1]);
        }
        else ret.push_back(prev);
        return ret;
    }
};
时间: 2025-02-01 23:51:27

148. Sort List (List)的相关文章

Leetcode 148. Sort List 归并排序 in Java

148. Sort List Total Accepted: 81218 Total Submissions: 309907 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *

No.148 Sort List

No.148 Sort List Sort a linked list in O(n log n) time using constant space complexity. 分析: 常量空间且O(nlogn)时间复杂度,单链表适合用归并排序,双向链表适合用快速排序 有一个问题是:若算上栈空间,空间复杂度也为O(nogn) 还是对排序算法不够熟练!! 1 struct ListNode 2 { 3 int val; 4 ListNode *next; 5 ListNode(int x):val(

[Lintcode]98. Sort List/[Leetcode]148. Sort List

98. Sort List/148. Sort List 本题难度: Medium Topic: Linked List Description Sort a linked list in O(n log n) time using constant space complexity. Example Example 1: Input: 1->3->2->null Output: 1->2->3->null Example 2: Input: 1->7->2

148. Sort List(js)

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题意:链表排序,时间复杂度nlogn代码如下:

刷题148. Sort List

一.题目说明 题目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*

148. Sort List

Sort a linked list in O(n log n) time using constant space complexity. 用mergeSort.快排的时间复杂度最差是n^2; /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Soluti

148. Sort List -- 时间复杂度O(n log n)

Sort a linked list in O(n log n) time using constant space complexity. 归并排序 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode *sortList(ListNode *head) { if (head==NULL || head->next == NULL){ return head;

[LeetCode#148]Sort List

The problem: Sort a linked list in O(n log n) time using constant space complexity. My analysis: The idea behind this problem is easy : merge sort !But we should learn some tricky skills from this question. 1. How to split a linked list into two sepa

148. Sort List (java 给单链表排序)

题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn),空间复杂度是O(1).时间复杂度为O(nlogn)的排序算法有快速排序和归并排序, 但是,对于单链表来说,进行元素之间的交换比较复杂,但是连接两个有序链表相对简单,因此这里采用归并排序的思路. 编码: public ListNode sortList(ListNode head) { if(hea

[LeetCode] 148. Sort List 解题思路

Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn),额外空间为 O(1). O(n*logn) 时间排序算法,无法是 quick sort, merge sort, head sort.quick sort 需要灵活访问前后元素,适合于数组,merge sort 只需要从左到右扫过去即可,可用于列表结构. 当列表元素个数大于2时,将列表拆分为左右