【链表】Sort List(归并排序)
题目:
Sort a linked list in O(n log n) time using constant space complexity.
思路:
nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,单向链表适合用归并排序。
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var sortList = function(head) {
if(head==null||head.next==null){
return head;
}else{
var slow=head,fast=head;
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
}
//拆成两个链表
fast=slow;
slow=slow.next;
fast.next=null; fast=sortList(head);
slow=sortList(slow);
return merge(fast,slow);
}
}; function merge(head1,head2){
if(head1==null){
return head2;
}
if(head2==null){
return head1;
}
var res=new ListNode(),p=new ListNode();
if(head1.val<head2.val){
res=head1;
head1=head1.next;
}else{
res=head2;
head2=head2.next;
}
p=res; while(head1!=null&&head2!=null){
if(head1.val<head2.val){
p.next=head1;
head1=head1.next;
}else{
p.next=head2;
head2=head2.next;
}
p=p.next;
} if(head1!=null){
p.next=head1;
}else if(head2!=null){
p.next=head2;
} return res;
}
【链表】Sort List(归并排序)的更多相关文章
- Sort List——经典(链表中的归并排序)
Sort a linked list in O(n log n) time using constant space complexity. 对一个链表进行排序,且时间复杂度要求为 O(n lo ...
- 148 Sort List 链表上的归并排序和快速排序
在使用O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 详见:https://leetcode.com/problems/sort-list/description/ Java实 ...
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- LeetCode 148 Sort List 链表上的归并排序和快速排序
Sort a linked list in O(n log n) time using constant space complexity. 单链表排序----快排 & 归并排序 (1)归并排 ...
- [SOJ]Easy sort (归并排序)
Description You know sorting is very important. And this easy problem is: Given you an array with N ...
- [Swift]LeetCode148. 排序链表 | Sort List
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- Natural Merge Sort(自然归并排序)
This is a Natural Merge Sort program from my textbook. It works, but I don't think it's good. // Nat ...
- js实现冒泡排序(bubble sort)快速排序(quick sort)归并排序(merge sort)
排序问题相信大家都比较熟悉了.用js简单写了一下几种常用的排序实现.其中使用了es6的一些语法,并且不仅限于数字--支持各种类型的数据的排序.那么直接上代码: function compare (a, ...
- Sort List 典型链表
https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...
随机推荐
- MemCachedClient 节点失效时的处理
引入jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...
- JS数组去重算法实现
之前一段时间一直在准备面试, 因而博客太久没更新: 现在基本知识点都复习完毕, 接下来就分享下 面试的一些常见问题: 去正规的互联网公司笔试.面试有很大的概率会碰到 使用javascript实现数组去 ...
- Hdu1361&&Poj1068 Parencodings 2017-01-18 17:17 45人阅读 评论(0) 收藏
Parencodings Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 建立多人协作git仓库/git 仓库权限控制(SSH)
转载文章请保留出处 http://blog.csdn.net/defeattroy/article/details/13775499 git仓库是多人协作使用的,可以基于很多种协议,例如http.g ...
- AXI总线(转)
AXI(Advanced eXtensible Interface)是一种总协议,该协议是ARM公司提出的AMBA(Advanced Microcontroller Bus Architecture) ...
- Android-XML格式描述
XML是W3C公司提出的标准,使用范围非常广阔,在框架的配置,程序的配置,布局文件的定义,网络传输等,无所不在: 以前学Java的时候,对XML的名词定义是,根节点,子节点 等等,而在Android里 ...
- Java子父类中的构造函数实例化过程
其实我们发现子类继承父类操作很简单,如果要是去深入的研究下会发现,实例化过程并非是我们看到的那样,我们就以代码举例来说明: 问大家,以下代码执行会输出什么呢? package com.oop; /** ...
- StructuredStream StateStore机制
ref: https://jaceklaskowski.gitbooks.io/spark-structured-streaming/ StruncturedStream的statefule实现基于S ...
- 手动处理TFS数据仓库服务和分析服务
当您需要报告中最新的数据时,当发生错误时,或者在解决了模式冲突之后,您可以手动处理Team Foundation Server(TFS)关系数据库(TFSHStor)或SQLServer Analys ...
- SQL分组合并
STUFF ( character_expression , start , length ,character_expression ) select TcodMedInst_GUID,stuff( ...