Question

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

Solution

分析,时间复杂度要求为nlogn,因此得考虑归并排序,但是空间必须为常量,因此得注意指针的操作。

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 head;
if (head->next == NULL)
return head;
ListNode* p1 = head;
ListNode* p2 = head;
ListNode* pre = head; // fast and slow 两个指针,用于找到中间节点
// pre节点,需要把链表断开,生成两个链表
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); }
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 List

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

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

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

  3. [LeetCode] Sort List 链表排序

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

  4. [LeetCode] Sort Colors 颜色排序

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

  5. [leetcode]Sort Colors @ Python

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

  6. LeetCode: Sort List 解题报告

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

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

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

  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 Transformed Array

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

  10. leetcode sort List

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

随机推荐

  1. HDU3342有向图判圈DFS&&拓扑排序法

    HDU3342 Legal or Not 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目意思:一群大牛互相问问题,大牛有不会的,会被更厉害 ...

  2. Mac自带Apache和Php

    Mac 是默认安装 apache和php,但是需要使用root用户来启用,所以请按照我下面的步骤来: 一.启用root用户1.选取系统偏好设置....2.从显示菜单中,选取“帐户”.3.点按锁图标并使 ...

  3. Service Mesh服务网格:是什么和为什么

    Service Mesh服务网格:是什么和为什么 - 好雨云帮 CSDN 博客 - CSDN博客 https://blog.csdn.net/zyqduron/article/details/8043 ...

  4. green rgb(255, 102, 0) #FF6600

    w通过元素背景色定位元素,改变其属性. style="background-color: #FF6600" <script> var w = document.quer ...

  5. Redis的基本使用(基于maven和spring)

    使用redis基本测试 maven导包 <dependency> <groupId>redis.clients</groupId> <artifactId&g ...

  6. 数据结构(java语言描述)

    概念性描述与<数据结构实例教程>大同小异,具体参考:http://www.cnblogs.com/bookwed/p/6763300.html. 概述 基本概念及术语 数据 信息的载体,是 ...

  7. 剑指Offer——二叉搜索树的后序遍历序列

    题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 分析: 二叉查找树(Binary Search ...

  8. CF#301 D:Bad Luck Island (概率dp)

    D:Bad Luck Island 一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j] ...

  9. sql 自定义函数返回中文,数字,英文

    --提取数字IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULLDROP FUNCTION DBO.GET_NUMBER2GOCREATE FUNCTION DBO. ...

  10. SQL Server自定义字符串分割函数——Split

    我相信大部分人都碰到过,处理数据的时候,字段的值是以 ',' (逗号)分隔的形式,所以我也不能避免. 然后我才知道,sql 是没有类似于 C# 和 Javascript 这种分割字符串的方法.( Sp ...