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)的时间复杂度和 ...
随机推荐
- Java实现ajax
jsp端的代码,sucess:function(){} 里面就是返回的处理 function ChangeTime(){ alert("www"); var startYmd = ...
- Android Cursor类的概念和用法
http://www.2cto.com/kf/201109/103163.html 关于 Cursor 在你理解和使用 Android Cursor 的时候你必须先知道关于 Cursor 的几件事情: ...
- PHP上传原理及应用
概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...
- TOM大师脚本-show space 多个版本,谢谢大牛们
示例一 该脚本需区分 对象的管理方式是 自动还是 手动, 对手动管理方式 的表显示很全面 SQL> exec show_space_old('MAN_TAB','DEV','TABLE'); F ...
- redhat6.4 配置centos6 yum替换
1.卸载掉系统redhat自带的yum rpm -qa |grep yum |xargs rpm -e --nodeps 2 下载相关的centos yum插件 主要有python-inipa ...
- 内联式css样式,直接写在现有的HTML标签中
CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式. 内联式css样式表就是把css代码直接写在现有的HTML标签中 ...
- 一条sql语句循环插入N条不同记录(转)
SET NOCOUNT ON IF (OBJECT_ID('TB' ) IS NOT NULL ) DROP TABLE TB GO CREATE TABLE TB(ID INT IDENTITY ( ...
- Prototype 模式
Prototype 模式提供了一个通过已存在对象进行新对象创建的接口(Clone) ,Clone()实现和具体的实现语言相关,在 C++中我们将通过拷贝构造函数实现之. /////////////// ...
- SGU 130.Circle
答案为Catalan数C(2k, k)/(k+1) #include <stdio.h> using namespace std; int k; int main() { scanf(&q ...
- css中文字体乱码解决方案
css中文字体乱码解决方案:把css编码和html页面编码统一起来.如果html页面是utf-8.css.js也统一成utf-8编码.还有一个避免中文乱码的办法就是把中文字体写成英文来表示 css中文 ...