【链表】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 ...
随机推荐
- Word图片上传控件(WordPaster)更新-2.0.15版本
更新说明: 1. 增加对webp图片的支持,支持微信公众号图片的下载. 效果参考:http://www.ncmem.com/doc/view.aspx?id=9761f8ce4fe04d0ab0f ...
- webservice大文件怎么传输
版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...
- 西邮Linux兴趣小组2014级免试挑战题 (续)
在上一篇的博客中已经解到第四关了,现在继续挑战-- [ 第四关] 在上一关解压成功后,生成了一个file文件.用vim的二进制格式打开,转成十六进制,发现文件头格式如下: 是个以ELF字符开头的文件, ...
- 16、Docker的网络-host和none
16.1 none 创建一个容器使用网络none: [root@docker ~]# docker run -d --name test1 --network none busybox /bin/ ...
- ArcGIS下图层范围不正确的两种处理方式
ArcGIS下图层范围不正确,偶尔能碰上这种情况,主要表现为“缩放至图层”时,其显示范围与该图层内所有要素的外包围盒范围不一致.针对这个问题,有两种解决办法. 方法一:导出数据.新创建含有要素的Sha ...
- Restframework 分页器 Pagnation 组件实例-5
分页逻辑 from rest_framework.pagination import PageNumberPagination class BookView(APIView): # authentic ...
- docker 搭建Mysql集群
docker基本指令: 更新软件包 yum -y update 安装Docker虚拟机(centos 7) yum install -y docker 运行.重启.关闭Docker虚拟机 servic ...
- jzoj5906
我們首先發現,每一條邊都至少走1次,因為我們必須走到每一個節點按按鈕 如果我們不走一個節點,說明這個節點已經有傳送門了,但是必須走到這個節點開傳送門,矛盾 然後我們發現,每一條邊至多經過2次 如果我們 ...
- Mysql的Text和Blob的比较
MySQL存在text和blob: (1)相同 在TEXT或BLOB列的存储或检索过程中,不存在大小写转换,当未运行在严格模式时,如果你为BLOB或TEXT列分配一个超过该列类型的最大长度的值,值被截 ...
- C++中vector的使用
在c++中,vector是一个十分有用的容器. 作用:它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据. vector在C++标准模板库中 ...