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))的时间复杂度和常数的空间复 ...
随机推荐
- 一个Web结合Mybatis项目
需要引入apache.commons.dbcp-1.2.2.osgi.jar以及org.apache.commons.pool-1.5.3.jar用来提供JDBC的访问: 需要org.springfr ...
- 51nod 1450 闯关游戏——期望dp
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450 想了半天,不知道不能走的状态(即最后不足m个的状态)怎么办. ...
- Send Code to evernote by my specify notebook
#coding:utf-8 import sys sys.path.append("lib") import thrift.protocol.TBinaryProtocol as ...
- Poj1218_THE DRUNK JAILER(水题)
一.Description A certain prison contains a long hall of n cells, each right next to each other. Each ...
- 资料:MVC框架+SQL Server 数据集成引擎
ylbtech-资料:MVC框架+SQL Server 数据集成引擎 1.返回顶部 1. 功能特点: MVC框架耦合性低视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样 ...
- 批量创建10个系统帐号tianda01-tianda10并设置密码
#.添加用户 useradd tianda01 #.非交互式给密码 echo "pass"|passwd --stdin tianda #.- 加0思路 ()..} () #随机密 ...
- play的job执行方式
除了使用Quartz CRON trigger, 还可以写一个action来专门触发job,这样子就可以随时启动job的开始,而且还能并行其他的任务.较方便.
- 20、BLAST比对及结果介绍
1.formatdb -i /share/nas1/huangt/project/IsoSeq/BMK170104-E545-03-a/Analysis_T01/MoveRebundant/T01/c ...
- PAM认证
PAM认证 摘自: http://www.cnblogs.com/shenxm/p/8451889.html PAM(Pluggable Authentication Modules) Sun公司于1 ...
- Entity Framework Code-First(3):Setup Environment
Setup Development Environment for EF Code-First: Let's setup the development environment for Code-Fi ...