148 Sort List 链表上的归并排序和快速排序
在使用O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
详见:https://leetcode.com/problems/sort-list/description/
Java实现:
链表上的归并排序:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode slow=head;
ListNode fast=head;
ListNode mid=null;
while(fast!=null&&fast.next!=null){
mid=slow;
slow=slow.next;
fast=fast.next.next;
}
mid.next=null;
return mergeSort(sortList(head),sortList(slow));
}
private ListNode mergeSort(ListNode low,ListNode high){
ListNode helper=new ListNode(-1);
ListNode cur=helper;
while(low!=null&&high!=null){
if(low.val<high.val){
cur.next=low;
low=low.next;
}else{
cur.next=high;
high=high.next;
}
cur=cur.next;
}
cur.next=low!=null?low:high;
return helper.next;
}
}
链表上的快速排序:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null){
return head;
}
quickSort(head,null);
return head;
}
private void quickSort(ListNode begin,ListNode end){
if(begin!=end){
ListNode seq=getSeperator(begin,end);
quickSort(begin,seq);
quickSort(seq.next,end);
}
}
private ListNode getSeperator(ListNode begin,ListNode end){
ListNode first=begin;
ListNode second=begin.next;
int pivot=first.val;
while(second!=end){
if(second.val<pivot){
first=first.next;
swap(first,second);
}
second=second.next;
}
swap(first,begin);
return first;
}
private void swap(ListNode a,ListNode b){
int tmp=a.val;
a.val=b.val;
b.val=tmp;
}
}
148 Sort List 链表上的归并排序和快速排序的更多相关文章
- LeetCode 148 Sort List 链表上的归并排序和快速排序
Sort a linked list in O(n log n) time using constant space complexity. 单链表排序----快排 & 归并排序 (1)归并排 ...
- [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 ...
- Sort List——经典(链表中的归并排序)
Sort a linked list in O(n log n) time using constant space complexity. 对一个链表进行排序,且时间复杂度要求为 O(n lo ...
- Insertion Sort List——链表的插入排序
Sort a linked list using insertion sort. 这道题跟 Sort List 类似,要求在链表上实现一种排序算法,这道题是指定实现插入排序.插入排序是一种O(n^2) ...
- 148. Sort List - LeetCode
Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...
- [LintCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
随机推荐
- js中的关系操作符
1.关于“小于(<)” a.关于数值的判断就是跟正常的比较一样: b.var result = “23” <"3" ; //true 这两个是字符串,会比较他们的编码 ...
- 通过powershell操作eventlog
relevant command list ~\Desktop> (Get-Command Write-EventLog).Parameters Key Value --- ----- Warn ...
- HashSe、LinkedHashSet、TreeSet(java基础知识十七)
1.HashSet存储字符串并遍历 * 特点:无序.无索引.无重复 HashSet存储字符串并遍历 HashSet<String> hs = new HashSet<>(); ...
- GDUT 积木积水 2*n 时间复杂度
题意 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元,于是他把这个三维的现实问 ...
- POJ3020 二分图匹配——最小路径覆盖
Description The Global Aerial Research Centre has been allotted the task of building the fifth gener ...
- [Selenium] Explicit wait 方法
(1) new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator)); (2) ne ...
- Objective-C中的+initialize和+load
写在前面 近几天花了一些时间了解了一下Objective-C runtime相关的东西,其中涉及到了+load方法,譬如method swizzling通常在category的+load方法中完成.之 ...
- 【NOIP2017 DAY1T2】 时间复杂度
[题目链接] 点击打开链接 [算法] 其实这就是一道模拟题啦! 在判error和计算时间复杂度时,我们需要用栈这种数据结构 [代码] 这题的代码还是有些难写的,写的时候一定要有条理! #include ...
- 多线程-threading模块2
从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个 t(t1.t2....),如果创建的线程较多时这样极其不方便.下面对通过例子进行改进: #coding:utf-8 impor ...
- caffe 入门实例3 fine-turning
占坑,使用fine-turning初始化参数...