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.net页面上得到Castle容器的实例

    在项目中使用Castle IOC容器,Asp.net程序中如何得到Castle容器内. 可以如下实现: 1.Gloabal实现接口IContainerAccessor public class Glo ...

  2. 1.2_php验证码

    使用php生成动态的验证码图片 <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...

  3. git、git bash、git shell的区别

    之前安装了github(CSDN上找的,官网的下不来,貌似要FQ - -)后,自带了git shell,如图: 输命令的时候发现网上的一些命令不管用,譬如:git ls –a 查看隐藏的 .git 文 ...

  4. 锚点链接 阻止a标签跳转

      参考 http://blog.csdn.net/awe5566/article/details/22583699 href="#downJacket" 锚点链接 必须写: 但又 ...

  5. 【Linux】命令学习笔记和总结

    莫名的想学习一下Linux了,因为对这方面的知识储备为0.对于命令行界面始终是零接触零了解,对一个程序员来说这几乎是致命的,所以简单了解一下. 一.教程参考 参考菜鸟教程即可: Linux 教程 | ...

  6. VB 十六进制转汉字的函数

    Public Function HexToStr(ByVal strs As String) As String Dim i As Integer, tmp As String, n If Len(s ...

  7. jsp页面上读取MySQL数据库datetime时间显示问题

    mysql数据库中时间字段选用了datetime,如果通过java实现在jsp页面上显示时间为"年-月-日  时:分"等格式,那么如下代码就会有不同的结果! 实体类中两个变量: p ...

  8. MySQL小记

    一.MyISAM和InnoDB MyISAM引擎是不支持事务的,所以一般开发Mysql的引擎使用InnoDB. 事务处理上方面: MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快,但 ...

  9. tagName()方法详解

    1.该方法可以通过元素的标签名称来查找元素.这个方法搜索到的元素通常不止一个,所以一般建议结合使用findElements方法来使用: driver.findElement(By.xpath(&quo ...

  10. python单线程解决并发

    1.单线程解决并发 方式一 import socket import select # 百度创建连接:非阻塞 client1 = socket.socket() client1.setblocking ...