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;
    }
    //find the middle place
    ListNode *p1=head, *p2=head->next; 

    while(p2 && p2->next){
        p1 = p1->next;
        p2 = p2->next->next;
    }
    p2 = p1->next;
    p1->next = NULL;
    return mergeTwoLists(sortList(head), sortList(p2));
}

ListNode *mergeTwoLists(ListNode* head1, ListNode* head2){
    ListNode *p1 = head1,  *p2=head2;
    static ListNode dummy(0);

    ListNode *tail = &dummy;

    while(p1 && p2){
        if(p1->val < p2->val){
            tail->next = p1;
            p1 = p1->next;
        }else{
            tail->next = p2;
            p2 = p2->next;
        }
        tail = tail->next;
    }
    if (p1) tail->next = p1;
    if (p2) tail->next = p2;

    return dummy.next;
}
时间: 2024-08-05 06:41:51

148. Sort List -- 时间复杂度O(n log n)的相关文章

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代码如下:

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; *

刷题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*

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。你可以假设 nums1 和 nums2 不会同时为空。

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 不会同时为空. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 示例 1: nums1 = [1, 3] nums2

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 (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时,将列表拆分为左右