Leetcode:Sort List

题目:Sort List

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

看题目有两个要求:1)时间复杂度为O(nlogn);2)空间复杂度为常数,即不能增设额外的空间。
满足这样要求的排序算法,我们首先想到快排,合并排序和堆排序。我们来分析下几种排序算法对时间和空间复杂度的要求,堆排序实现上过于繁琐,我们不做考虑。快排的最坏的时间复杂度是O(n^2),平均复杂度为O(nlgn),如果按照题目的严格要求显然快排是不满足的,而且快排的实现引入了递归操作,递归调用的栈空间严格意义上说也是额外空间。另外值得注意的一点是:链表不像数组一样,可以随机访问元素,链表必须顺序访问,所以一般的递归操作很难实现,虽然也可以实现哈,见该文:递归实现链表排序。对于归并排序,我们知道需要O(n)的空间复杂度,即需要一个临时数组来存放排好序的元素,显然也合理,但那是针对的是数组,对于链表,归并排序的空间复杂度为in-place sort,即不需要额外空间就可以完成。另外,归并排序还有一个比较好的优势是其稳定性。所以,对于本题的解法,我们首选归并排序。

归并排序有多种方式,总的来说有三种,1)递归;2)非递归;3)自然合并;详见本文:归并排序的三种实现方法。对于链表,采用非递归的方式更为高效,用以下的一幅图来说明非递归的方式:

将两两子列表进行合并组合,达到排序的目的。本题的代码如下,参考上文实现的。

 1 ListNode *sortList(ListNode *head)
 2 {
 3     assert(NULL != head);
 4     if (NULL == head)
 5         return NULL;
 6
 7     ListNode *p = head;
 8     int len = 0;        //get the length of link
 9     while (NULL != p) {
10         p = p->next;
11         len ++;
12     }
13
14     ListNode *temp = new ListNode(0);
15     temp->next = head;
16
17     int interval = 1;   //合并子列表的长度
18     for (; interval <= len; interval *= 2) {
19         ListNode *pre = temp;
20         ListNode *first = temp->next;  //两段子列表的起始位置
21         ListNode *second = temp->next;
22
23         while (NULL != first || NULL != second) {
24             int i = 0;
25             while (i < interval && NULL != second) {
26                 second = second->next; //将second移到第二段子列表的起始处
27                 i ++;
28             }
29
30             int fvisit = 0, svisit = 0; //访问子列表的的次数<interval,保证列表中的元素全部能被访问
31             while (fvisit < interval && svisit < interval && NULL != first && NULL != second) {
32                 if (first->val < second->val) {
33                     pre->next = first;
34                     pre = first;
35                     first = first->next;
36                     fvisit ++;
37                 }
38                 else {
39                     pre->next = second;
40                     pre = second;
41                     second = second->next;
42                     svisit ++;
43                 }
44             }
45             while (fvisit < interval && NULL != first) {
46                 pre->next = first;
47                 pre = first;
48                 first = first->next;
49                 fvisit ++;
50             }
51             while (svisit < interval && NULL != second) {
52                 pre->next = second;
53                 pre = second;
54                 second = second->next;
55                 svisit ++;
56             }
57             pre->next = second;
58             first = second;
59         }
60     }
61     ListNode *retResult = temp->next;
62     delete temp;
63     temp = NULL;
64     return retResult;
65 }
时间: 2024-08-08 01:20:35

Leetcode:Sort List的相关文章

leetcode:Sort Colors

一.     题目 给一个数组包含n个物体,有蓝色.红色和白色三种颜色,把他们分类并按照红.白.蓝的顺序排列,我们用0.1.2来表示红白蓝的颜色 注解:很容易想到遍历两遍数组得到三个数的数目,再覆盖,但是请只遍历一遍数组来解决. 二.     分析 很简单,题目的意思其实就是让对一个数组排序,数组中的元素只有0.1.2,并且要求只能遍历一遍数组,常数空间复杂度.此时我们可以想到借助于快速排序的partition思想,以1为中间枢纽对数组进行划分,使0在数组的左边,2在数组的右边,1在数组的中间.

【LeetCode】 sort list 单链表的归并排序

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排序,链表找到中点的方法 存在的缺点:边界条件多考虑!!! /** * LeetCode Sort List Sort a linked list in O(n log n) time using constant space complexity. * 题目:将一个单链表进行排序,时间复杂度要求为o

【LeetCode】Sort Colors

LeetCode OJ 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, w

[leetcode]Insertion Sort List @ Python

原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科. 代码循环部分图示: 代码: class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if not head: return head dummy = Lis

[LeetCode OJ] 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, and bl

LeetCode :: Insertion Sort List [详细分析]

Sort a linked list using insertion sort. 仍然是一个非常简洁的题目,让我们用插入排序给链表排序:这里说到插入排序,可以来回顾一下, 最基本的入门排序算法,就是插入排序了:时间复杂度为n^2,最基本的插入排序是基于数组实现的,下面给出基于数组实现的插入排序,来体会一个插入排序的思想: 以下仅为数组实现,不是解题代码,没兴趣可以跳过. void insertionsort (int a[], int N) { for (int i = 1; i < N; i+

leetcode 专题—sort

此将主要将leetcode中sort专题的解答都放在这里,后续会慢慢加入 一:leetcode179 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 re

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

LeetCode OJ - Sort List

题目: Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 复杂度为O(n* logn) 的排序算法有:快速排序.堆排序.归并排序.对于链表这种数据结构,使用归并排序比较靠谱.递归代码如下: 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNod