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 ...
随机推荐
- Mac OS用docker Desktop安装单节点kubernetes
方案: 安装方式:阿里云minikube,k8s官方minikube,kubeadm, docker Desktop中自带第k8s 安装环境:在linux虚拟机中安装k8s,在macos中安装k8s, ...
- hibernate 的分页查询
hibernate的分页查询有个好处,就是不用管数据库方言.比如db2的分页查询很麻烦,但是用hibernate的方式,就完全不用管这些了 /* 使用HQL分页查询Customer信息 */ publ ...
- D. Babaei and Birthday Cake--- Codeforces Round #343 (Div. 2)
D. Babaei and Birthday Cake time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Java 8 新的时间日期 API
1. 概述 1.1 简介 Java 8 引入了一套全新的时间日期API,操作起来更简便.简单介绍下,LocalDate和LocalTime和LocalDateTime的使用: java.util.Da ...
- leelazero and google colab
https://github.com/gcp/leela-zero/blob/master/COLAB.md 左侧菜单展开,可以查看细节
- mybatis使用序列批量插入数据
mybatis只提供了单条数据的插入,要批量插入数据我们可以使用循环一条条的插入,但是这样做的效率太低下,每插入一条数据就需要提交一次,如果数据量几百上千甚至更多,插入性能往往不是我们能接受的,如下例 ...
- lucene 5的测试程序——API变动太大
package hello; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import ...
- linux编程 fmemopen函数打开一个内存流 使用FILE指针进行读写访问
fmemopen()函数打开一个内存流,使你可以读取或写入由buf指定的缓冲区.其返回FILE*fp就是打开的内存流,虽然仍使用FILE指针进行访问,但其实并没有底层文件(并没有磁盘上的实际文件,因为 ...
- Opencv实现两幅图像融合
实现两幅图像线性(不同系数下)的融合涉及到Opencv中两个关键的方法,addWeighted()和createTrackbar() addWeighted方法: 函数原型: void addWeig ...
- [Java] public, private, protected
同包不同类的成员 不同包中子类的成员 不同包非子类的成员 public √ √ √ protected √ √ × 默认 √ × × private × × ×