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

题目要求在常数控件内以O(nlogn)的事件复杂度来排序链表。

常数空间内我没有实现,O(nlogn)的话使用归并排序就可以了吗, 下面是代码:

 /**
* 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 || !head->next)
return head;
return mergeSort(head);
} ListNode * mergeSort(ListNode * head)
{
if(head == NULL || head->next == NULL) return head;
ListNode * fastP = head;
ListNode * slowP = head;
ListNode * slowPre = slowP; //这里生成快慢指针的时候应该注意一点,选一个pre节点,否则直接用slowP的话那么分成的两段是不平衡的
for(; fastP != NULL && fastP->next != NULL; fastP = fastP->next->next, slowP = slowP->next){
slowPre = slowP;
}
ListNode * head1 = head;
ListNode * head2 = slowP;
slowPre->next = NULL;//截断list
head1 = mergeSort(head1);
head2 = mergeSort(head2);
return merge(head1, head2);
} ListNode * merge(ListNode * head1, ListNode * head2)
{
ListNode * ret = new ListNode(); //记录首节点的位置
ListNode * helper = ret; //这里应该注意,helper是用来标记下一个插入位置用的。
while(head1 && head2){
if(head1->val < head2->val){
helper->next = head1;
head1 = head1->next;
}else{
helper->next = head2;
head2 = head2->next;
}
helper = helper->next;//指向当前链表的尾节点
}
if(head1 == NULL)
helper->next = head2;
else
helper->next = head1;
helper = ret->next;
ret->next = NULL; //销毁helper节点
delete ret;
return helper;
} };

下面的事java版,写了很多的局部变量,主要事图个方便啊,方法与上相同,可以满足题目的限制条件。代码如下所示:

 public class Solution {
public ListNode sortList(ListNode head) {
if(head == null || head.next == null)
return head;
return mergeSort(head);
} public ListNode mergeSort(ListNode head){
if(head == null || head.next == null) return head;
ListNode fast = head;
ListNode slow = head;
ListNode slowPre = new ListNode(-1);
slowPre.next = head;
while(fast != null){
fast = fast.next;
slow = slow.next;
slowPre = slowPre.next;
if(fast != null)
fast = fast.next;
}
ListNode list1 = head;
ListNode list2 = slow;
slowPre.next = null;
list1 = mergeSort(list1);
list2 = mergeSort(list2);
return merge(list1, list2);
} public ListNode merge(ListNode head1, ListNode head2){
if(head1 == null) return head2;
if(head2 == null) return head1;
ListNode helperP = new ListNode(-1);
helperP.next = head1;
ListNode pPre = helperP;
ListNode p = head1;
ListNode helperQ = new ListNode(-1);
helperQ.next = head2;
ListNode qPre = helperQ;
ListNode q = head2;
while(p != null && q != null){
if(p.val < q.val){
p=p.next;
pPre = pPre.next;
}else{
pPre.next = q;
qPre.next = q.next;
q.next = p;
q = qPre.next;
pPre = pPre.next;
}
}
if(p == null){
pPre.next = helperQ.next;
helperQ.next = null;
}
return helperP.next;
}
}

LeetCode OJ:Sort List(排序链表)的更多相关文章

  1. LeetCode - 82、删除排序链表中的重复元素 II

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5    输出: 1 ...

  2. LeetCode - 83、删除排序链表中的重复元素

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2    输出: 1->2 示例 2: 输入: 1->1->2->3 ...

  3. [LeetCode] Wiggle Sort 摆动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  4. [LeetCode 109] - 将已排序链表转换为二叉搜索树 (Convert Sorted List to Binary Search Tree)

    问题 给出一个元素以递增序列排序的单链表,将其转换为一棵高度平衡的二叉搜索树. 初始思路 二叉搜索树高度平衡,意味着左右子树的高度要平衡.根据二叉树左子树节点小于根节点,右子树节点大于根节点的性质:我 ...

  5. [Leetcode] 第148题 排序链表

    一.题目描述 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3 输出: 1->2->3->4 示 ...

  6. Leetcode148. Sort List排序链表

    在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3 输出: 1->2->3->4 示例 2: 输入 ...

  7. [LeetCode OJ] Sort Colors

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

  8. leetcode——Insertion Sort List 对链表进行插入排序(AC)

    Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNo ...

  9. [LeetCode题解]83. 删除排序链表中的重复元素 | 递归 + 迭代

    方法一:递归 解题思路 通过递归法,每次判断目前头节点与给定的节点是否相等.如是,继续判断下一个节点,否则保存当前头节点,设置 next 指向下次递归得到的节点,然后返回当前节点. 代码 /** * ...

  10. LeetCode【83. 删除排序链表中的重复元素】

    我最开始的程序是 但是结果

随机推荐

  1. boost之日期date_time

    date_time库使用的日期基于格里高利历,支持从1400-01-01到9999-12-31的日期. 空的构造函数会创建一个值为not_a_date_time的无效日期:顺序传入年月日值则创建一个对 ...

  2. 运行jupyter notebook 出错 Error executing Jupyter command 'notebook'

    实际上是安装jupyter时候有错误, 仔细看日志发现需要缺少 Microsoft Visual C++ Compiler for Python 2.7 下载安装后,重新安装jupyter即可 htt ...

  3. C#数组存入引用类型

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cont ...

  4. openstack ocata版(脚本)控制节点安装

    一.初始化环境: 1.更换yum源: yum install -y wget mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS- ...

  5. iOS self 和 super 学习

    有人问我 这个问题 回答错了,题干大概是说 [self class] 和 [super class]打印结果 是不是一样的. 我睁着眼睛说是不一样的 .因为我明明记得 几天前 做 DFS 获取反射基类 ...

  6. Web前端开发的基本要求和认识

    Web前端开发技术包括三个要素:HTML.CSS和JavaScript,但随着RIA的流行和普及,Flash/Flex.Silverlight.XML和服务器端语言也是前端开发工程师应该掌握的.Web ...

  7. kafka connect简介以及部署

    https://blog.csdn.net/u011687037/article/details/57411790 1.什么是kafka connect? 根据官方介绍,Kafka Connect是一 ...

  8. $用python处理Excel文档(2)——用xlsxwriter模块写xls/xlsx文档

    Refer:<python自动化运维:技术与最佳实践> 更多用法参考xlsxwriter官方文档:http://xlsxwriter.readthedocs.io/ 本文主要总结一下如何使 ...

  9. [POI2007]驾驶考试egz

    题目 BZOJ 神仙题,可比那些氵紫题有意思多了 做法 \(i\)能作为起始点,当\(i\)能到达\(1\)~\(i-1\)和\(i+1\)~\(n\) 这样处理显然会麻烦,因为要从每个点都特判一次 ...

  10. INSPIRED启示录 读书笔记 - 第37章 大众网络服务产品

    十大要点 1.可用性:大众网络服务产品必须具备良好的用户体验 2.人物角色:按典型特征将用户分类,抽象出有代表性的用户类型(人物角色) 3.扩展性:应该不间断地考虑扩展性问题,永远留有余地,不到万不得 ...