【原创】leetCodeOj --- Sort List 解题报告

今日leetcode链表题全制霸

原题地址:

https://oj.leetcode.com/problems/sort-list/

题目内容:

Sort List

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

方法:

题目要求是链表排序,同时时间复杂度要求O(n log n),空间复杂度要求常数空间。这意味着你不可以把链表拷贝到数组中,用一个map保留值和指针的对应关系,最后在构造一个链表。

我们需要考察不同的排序算法,看看哪种排序算法可以处理链表。关键在于哪种排序算法对于随机访问的依赖度低。

快排:首位指针,排除

堆排:要有两个儿子,排除

那就只能用归并排序了。

这次先给代码再分析复杂度

全部代码:

/**
 * 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)
            return NULL;
        int len = countLength(head);
        if (len == 1)
            return head;
        int mid = len / 2;
        ListNode *sec = dividedLst(head,mid);
        head = sortList(head);
        sec  = sortList(sec);
        head = mergeList(head,sec);
        return head;
    }

    ListNode *mergeList(ListNode *lst1,ListNode *lst2)
    {
        if (!lst2)
            return lst1;
        if (!lst1)
            return lst2;
        ListNode *ptr_lst1 = lst1;
        ListNode *ptr_lst2 = lst2;
        ListNode *begin = (ListNode *)malloc(sizeof(ListNode));
        ListNode *tail  = begin;
        tail->next = NULL;
        while (ptr_lst1 && ptr_lst2)
        {
            if (ptr_lst1->val < ptr_lst2->val)
            {
                tail->next = ptr_lst1;
                ptr_lst1   = ptr_lst1->next;
                tail->next->next = NULL;
                tail = tail->next;
            }
            else
            {
                tail->next = ptr_lst2;
                ptr_lst2   = ptr_lst2->next;
                tail->next->next = NULL;
                tail = tail->next;
            }
        }
        if (!ptr_lst1 && ptr_lst2) // lst1 is empty and lst2 has some
            tail->next = ptr_lst2;
        if (!ptr_lst2 && ptr_lst1) // lst1 has some and lst2 is empty
            tail->next = ptr_lst1;
        return begin->next;
    }

    int countLength(ListNode *head)
    {
        int count = 0;
        while (head)
        {
            count ++;
            head = head->next;
        }
        return count;
    }

    ListNode *dividedLst(ListNode *lst1,int mid)
    {
        ListNode *pre = lst1;
        int count = 0;
        while (lst1)
        {
            if (mid == count)
            {
                pre->next = NULL;
                return lst1;
            }
            count ++;
            pre = lst1;
            lst1 = lst1->next;
        }
    }
};

sort函数中,计算长度是n,合并是n,找中点是n/2,常数个n还是n

因此时间复杂度就是O(n log n)

时间: 2024-11-12 04:53:13

【原创】leetCodeOj --- Sort List 解题报告的相关文章

【原创】leetCodeOj --- Largest Number 解题报告

原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may

【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority

【原创】leetCodeOj --- Dungeon Game 解题报告

原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was init

【原创】leetCodeOj ---Partition List 解题报告

原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in

LeetCode: Sort List 解题报告

Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度是 O(logN) 因为使用了栈空间. SOLUTION 1: 使用Merge Sort来解决问题. 为什么不用QuickSort? 因为随机访问对于链表而言太耗时,而heap sort不可行. 注意,Find Mid用了2种解法.或者是让Fast提前结束,或是让Fast先走一步,目的就是要取得中间

【LeetCode】Sort Colors 解题报告

[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, a

LeetCode: Sort Colors 解题报告

Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, wh

【原创】ZOJ_1649 Rescue 解题报告

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want

【原创】leetCodeOj --- Sliding Window Maximum 解题报告

天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the