Sort List ——LeetCode
Sort a linked list in O(n log n) time using constant space complexity.
链表排序,要求时间复杂度O(nlgn),我写的归并排序。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode sortList(ListNode head) {
if(head==null){
return head;
}
ListNode tail = head;
while(tail.next!=null){
tail=tail.next;
}
return mergeList(head,tail);
} ListNode mergeList(ListNode head,ListNode tail){
if(head==tail){
return head;
}
ListNode mid = getMid(head,tail);
ListNode postList = mergeList(mid.next,tail);
mid.next=null;
ListNode preList = mergeList(head,mid);
return merge(preList,postList);
} ListNode getMid(ListNode head,ListNode tail){
if(head==tail){
return head;
}
ListNode fast = head,slow = head;
while(fast.next!=null&&fast.next.next!=null&&fast.next!=tail){
fast=fast.next.next;
slow=slow.next;
}
return slow;
}
ListNode merge(ListNode l1,ListNode l2){
if(l1==null||l2==null){
return l1==null?l2:l1;
}
ListNode ptr = new ListNode(0);
ListNode head = ptr;
while(l1!=null&&l2!=null){
if(l1.val<=l2.val){
ptr.next = l1;
l1=l1.next;
}else{
ptr.next = l2;
l2=l2.next;
}
ptr=ptr.next;
}
ptr.next=l1==null?l2:l1;
return head.next;
}
}
Sort List ——LeetCode的更多相关文章
- Sort List leetcode
这个题一开始本想用快速排序的,但是想了20分钟都没有头绪,难点在于快速排序的随机访问无法用链表实现,不过如果可以实现快速排序partition函数就可以了,但是这可能比较复杂,于是改用其他排序方法,上 ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- Sort Colors [LeetCode]
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Sort Colors —— LeetCode
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Insertion Sort List —— LeetCode
Sort a linked list using insertion sort. 题目大意:将一个单链表使用插入排序的方式排序. 解题思路:先新建一个头指针,然后重新构建一下这个单链表,每次从头找到第 ...
- sort vector - leetcode 新用法
179. Largest Number sort(num.begin(), num.end(), [](int a, int b){ return to_string(a)+to_string(b) ...
- Insertion Sort List Leetcode
Sort a linked list using insertion sort. 这个题我巧妙的设置了一个临时头结点 class Solution { public: ListNode* insert ...
- Sort Colors leetcode java
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- Sort List leetcode java
题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和 ...
随机推荐
- noip 2003 传染病控制(历史遗留问题2333)
/*codevs 1091 搜索 几个月之前写的70分 今天又写了一遍 并且找到了错误 */ #include<cstdio> #include<vector> #define ...
- JS,JQuery杂谈
JS返回页面: JS返回前一个页面,经常看到有人用window.history.go(-1)这种方法 这种放的确可以返回,也仅仅只是返回,返回的页面信息却没有刷新.也有人用windows.histo ...
- Tomcat-java.lang.IllegalArgumentException: Document base F:apps does not exist or is not a readable
启动Tomcat的时候,报错:java.lang.IllegalArgumentException: Document base F:apps does not exist or is not a r ...
- copy file using FileReader/Writer.
The code below demonstates copying file using 'FileReader' and 'FileWriter'. class CopyV2 extends Ti ...
- SGU 146.The Runner
时间限制:0.25s 空间限制:4M 题意: 一个人在一个周长为L的圆上跑,每个时间段(Ti)的速度(Vi)不一样,问最后他离起点的圆弧距离,周长是个有四位小数的浮点数,其它全是整数. Solutio ...
- cos-26上传
在开发中常常需要上传文件,上传文件的方式有很多种,这里有一个cos实现的例子. 首先是要拷贝cos.jar包拷贝到WEB-INF/lib目录下,然后才进行编码. 创建一个可以进行自动重命名的Java文 ...
- 【转】linux 必须掌握的60个命令
Linux必学的60个命令Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要 ...
- MVC 文本转换成html显示
最近在学习ASP.NET MVC,项目中需要将后台传输的HTML文本在前台页面显示:@Html.Raw(HttpUtility.HtmlDecode(ViewBag.DisplayText)).记下来 ...
- [jQuery编程挑战]004 针对选择框词典式排序
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- 去除后台ckeditor的style="...."的样式
.cnt_text .text img {/*width :auto !important;*/height :auto !important; max-width:660px;} 高度自适应,高度让 ...