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. CentOS 7 64位 安装 PostgreSQL 9.2整理

    centos版本 LSB Version: :core-4.1-amd64:core-4.1-noarchDistributor ID: CentOSDescription: CentOS Linux ...

  2. 【BZOJ3207】花神的嘲讽计划Ⅰ Hash+主席树

    [BZOJ3207]花神的嘲讽计划Ⅰ Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快 ...

  3. oracle的row_number() OVER (ORDER BY COL2 asc)和row_number() OVER (PARTITION BY COL1 ORDER BY COL2)的用法

    转自:https://jingyan.baidu.com/article/9989c74604a644f648ecfef3.html SELECT ROW_NUMBER() OVER(PARTITIO ...

  4. python之MySQL学习——数据操作

    1.增 import pymysql as ps # 打开数据库连接 db = ps.connect(host=', database='test', charset='utf8') # 创建一个游标 ...

  5. 开机启动/etc/rc.local失效无效怎么办?解决方法

    开机启动/etc/rc.local失效问题的解决方法 第一种情况:当centOS 随机启动文件 /etc/rc.local 失效时请按如下修改1.echo /etc/rc.local >> ...

  6. gevent For the Working Python Developer

    Gevent指南   gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API. 介绍 贡献者 核心部分 Greenle ...

  7. django_上传文件

    要求:  写一个文件上传:如果文件名字重复,不要覆盖,并且放到项目根路径的media文件夹下 def upload(request): if request.method == "GET&q ...

  8. jquery补充

  9. DRF(3) - 序列化组件(GET/PUT/DELETE接口设计)、视图优化组件

    一.序列化组件 基于上篇随笔的表结构,通过序列化组件的ModelSerializer设计如下三个接口: GET 127.0.0.1:8000/books/{id} # 获取一条数据,返回值:{} PU ...

  10. windows下的Mysql安装与基本使用(zip)

    一.安装过程 Mysql社区版下载地址:http://dev.mysql.com/downloads/mysql/ --注意,已管理员身份运行cmd.exe,很重要!!目录在,c:\windows\s ...