LeetCode—-Sort List

Question

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

Solution

看到对链表的排序,时间复杂度O(n log n),首先想到的就是归并排序。 但是这里其中有两个技巧:

  1. 就是将两个链表分开的时候,用到了fast-slow法,这是在处理链表分治,也就是找中间节点的一种有效方法。
  2. 还有就是merge的过程,也用到了递归的方式。

Code

/**
* 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 == NULL)
return NULL;
if (head->next == NULL)
return head; ListNode* p1 = head;
ListNode* p2 = head;
ListNode* pre = head; // fast-slow 法
while (p2 != NULL && p2->next != NULL) {
pre = p1;
p1 = p1->next;
p2 = p2->next->next;
}
pre->next = NULL; ListNode* L1 = sortList(head);
ListNode* L2 = sortList(p1); return merge(L1, L2); }; // merge 链表的方法,也用到了递归
ListNode* merge(ListNode* L1, ListNode* L2) {
if (L1 == NULL)
return L2;
if (L2 == NULL)
return L1; if (L1->val <= L2->val) {
L1->next = merge(L1->next, L2);
return L1;
} else {
L2->next = merge(L1, L2->next);
return L2;
}
}
};

LeetCode—-Sort List的更多相关文章

  1. [LeetCode] Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  2. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  3. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  5. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  6. LeetCode Sort List 链表排序(规定 O(nlogn) )

    Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...

  7. [LeetCode] Sort Transformed Array 变换数组排序

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  8. Leetcode: Sort Transformed Array

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  9. leetcode sort List

    Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-l ...

随机推荐

  1. 《ASP.NET1200例》C#在网页上编写动态时钟

    包含Timer类的命名空间有3个 Timer Class (System.Threading)‎ Timer Class (System.Windows.Forms)‎ 一般用于窗体程序 Timer  ...

  2. servlet ; basepath ; sendredirected ;

    Eclipse 新建 jsp页面里自动生成以下代码: <%String path = request.getContextPath();String basePath = request.get ...

  3. 《从零开始学Swift》学习笔记(Day 62)——Core Foundation框架之内存托管对象与非托管对象

    原创文章,欢迎转载.转载请注明:关东升的博客 内存托管对象 Swift中调用Core Foundation函数获得对象时候,对象分为:内存托管对象和内存非托管对象. 内存托管对象就是由编译器帮助管理内 ...

  4. SetForegroundWindow以及 如何将一个某个窗口提到最顶层(转)

    http://hi.baidu.com/gookings/item/2b7912ca8d5b3625a0b50aa2 SetForegroundWindow 函数功能:该函数将创建指定窗口的线程设置到 ...

  5. CodeForces 663A Rebus

    A. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  6. delphi -----获取计算IP

    function GetHostName:String; var ComputerName: ..MAX_COMPUTERNAME_LENGTH+] of char; Size: Cardinal; ...

  7. 巨蟒python全栈开发-第15天 装饰器

    一.今日内容总览 关于函数的装饰器1.装饰器(重点,难点)(要求:反复写,代码不多但是很绕) 开闭原则:(比如,菜单是拆散的,一点点搞的,用友拆散自己的功能,以后就不用开发了) (1)对功能的扩展开放 ...

  8. css 特殊处理样式记录

    1.解决任何盒子的垂直横向 居中显示 display: -webkit-box; -webkit-flex: 1; -webkit-box-orient: vertical; -webkit-box- ...

  9. VS2010 / MFC + OpenCV 2.4.1打开图片

    Windows 7 x64,VS2010 / MFC + OpenCV 2.4.1打开图片显示到Picture控件中. OpenCV 2.2.OpenCV 2.3同样适用. 工具/原料 WinXP / ...

  10. [Android]反编译apk + eclipse中调试smali

    从来没有想过反编译apk是来的如此方便,并且还可以修改后重新编译运行,这比在win下修改pe容易多了,感谢apktool和smali工具的作者提供这么好的工具. 跟踪apk一般的做法是在反编译的sma ...