LeetCode 148 Sort List 链表上的归并排序和快速排序
Sort a linked list in O(n log n) time using constant space complexity.
单链表排序----快排 & 归并排序
(1)归并排序
/**
* 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 == nullptr || head->next == nullptr)
return head;
ListNode* slow = head;
ListNode* fast = head;
ListNode* mid = nullptr;
while (fast&&fast->next)
{
mid = slow;
slow = slow->next;
fast = fast->next->next;
}
mid->next = nullptr;
return mergeHelp(sortList(head), sortList(slow));
}
ListNode* mergeHelp(ListNode* low, ListNode* high)
{
ListNode* head = new ListNode(-);
ListNode* cur = head;
while (low&&high)
{
if (low->val < high->val)
{
cur->next = low;
low = low->next;
}
else
{
cur->next = high;
high = high->next;
}
cur = cur->next;
}
cur->next = low ? low : high;
return head->next;
}
};
(2)快速排序
①.使第一个节点为中心点;
②.创建2个指针(first,second),first指向头结点,second指向first的下一个节点;
③.second开始遍历,如果发现second的值比中心点的值小,则此时first=first.next,并且执行当前first的值和second的值交换,second遍历到链表尾即可;
④.把头结点的值和first的值执行交换。此时first节点为中心点,并且完成1轮快排
⑤.使用递归的方法即可完成排序检测是否合法
/**
* 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==nullptr)
return head;
quickSort(head,nullptr);
return head;
}
void quickSort(ListNode* begin,ListNode* end)
{
if(begin!=end)
{
ListNode* sep=getSeperator(begin,end);
quickSort(begin,sep);
quickSort(sep->next,end);
}
}
ListNode* getSeperator(ListNode* begin,ListNode* end)
{
ListNode* first=begin;
ListNode* second=begin->next;
int key=begin->val;
while(second!=end)
{
if(second->val<key)
{
first=first->next;
swap(first,second);
}
second=second->next;
}
swap(begin,first);
return first;
}
void swap(ListNode* a,ListNode* b)
{
int tmp=a->val;
a->val=b->val;
b->val=tmp;
}
};
LeetCode 148 Sort List 链表上的归并排序和快速排序的更多相关文章
- 148 Sort List 链表上的归并排序和快速排序
在使用O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 详见:https://leetcode.com/problems/sort-list/description/ Java实 ...
- [LeetCode]148. Sort List链表归并排序
要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- Leetcode#148 Sort List
原题地址 链表归并排序 真是恶心的一道题啊,哇了好多次才过. 代码: void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNo ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- leetcode 148. Sort List ----- java
Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...
随机推荐
- bzoj 2093 [Poi2010]Frog——滑动窗口
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2093 找第k近的可以用一个含k个元素的滑动窗口来实现. 卡空间也还行,但卡时间.不要预处理倍 ...
- 如何避免这个delete from tb_name不带条件的操作
那么,我们如何避免这个delete from tb_name不带条件的呢?其实是有办法的,但这只针对运维DBA或者DBA在操作时候有用,但对于PHP和JAVA程序,它的连接操作方式,就没办法避免了 s ...
- strcmp strcpy的使用 char类型
//判断从控制台输入的密码是否正确 BOOL varifyPassWord(char passWord[] , int index) { BOOL result = YES; int count = ...
- css基础知识一
1.CSS (Cascding Style Sheet)层叠样式表 级联样式表 样式表 2.CSS作用: 修改页面中元素的显示样式 能够实现内容与表现的分离 提高代码的可重用性和可维护性 3.导入CS ...
- 宽字符wchar_t和窄字符char区别和相互转换
转自:http://blog.csdn.net/nodeathphoenix/article/details/7416725 1. 首先,说下窄字符char了,大家都很清楚,就是8bit表示的b ...
- 手把手教你写Kconfig---基于tiny4412开发板
转自:http://blog.csdn.net/morixinguan/article/details/54744237 今天,我就来教大家写写最简单的Kconfig,什么是Kconfig? 我们配置 ...
- BadImageFormatException,未能加载正确的程序集XXX的解决办法
BadImageFormatException,未能加载正确的程序集XXX的解决办法 IDE:VS2010 语言:C# 异常:System.BadImageFormatException,未能加载正确 ...
- python tarfile模块基本使用
1.压缩一个文件夹下的所有文件 #coding=utf8 import os import tarfile __author__ = 'Administrator' def main(): cwd = ...
- Learning Python 003 缩进
Python 缩进 Python的代码中不使用{}大括号来来表示一个代码块,而是使用缩进方式.像下面这段代码: # print absolute value of an integer: a = 10 ...
- cocos2dx v3.x lua绑定分析
打算新项目转到cocos2dx v3上了,下载代码浏览过后发现改动真是非常大,结构性调整很多. 比如tolua绑定这一块,就几乎全翻新了. 胶水代码的生成,改成了全自动式的,通过clang来分析c++ ...