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)的时间复杂度和 ...
随机推荐
- 一次利用MSSQL的SA账户提权获取服务器权限
遇到小人,把服务器整走了 自己手里只有sql server的sa账号密码 模糊记起之前用这个账户提权读取文件的事 百度之,发现相关信息一堆堆 各种工具也用了不少 发现不是语法错误就是权限不够 无奈之下 ...
- Android开发中用友盟做分享的一些坑
仅限于用5.1.4版本的 按照友盟分享的API在自己的代码中修改: 1.微信分享需要打包APK文件,数字签名与微信开发申请的要一致 2.此name中属性不能修改 value为友盟的申请的appkey ...
- linux下启动oracle服务命令
以redflag(redhat /centos)linux下的 oracle 10g 为例: 如果oracle安装和配置都没有问题的话: 依次执行以下代码即可启动oracle服务. #su - ora ...
- PV、UV、IP的区别
网站推广需要一个网站访问统计工具,常用的统计工具有百度统计.51la.量子恒道统计等.网站访问量常用的指标为PV.UV.IP.那么什么是PV.UV和IP,PV.UV.IP的区别是什么? --首先来看看 ...
- Python:函数定义
#!/usr/bin/python3 #函数 def add(a,b): return a+b print("add(2,5) = ",add(2,5)) def add2(a,b ...
- pthread_rwlock_t读写锁函数说明
读写锁 索引: 初始化一个读写锁pthread_rwlock_init 读锁定读写锁 pthread_rwlock_rdlock 非阻塞读锁定 pthread_rwlock_tryrdloc ...
- 【POJ3468】【zkw线段树】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- dedecms 文章内容文章名字和文章网址的调用
文章标题: <a href="{dede:field name='arcurl'/}">{dede:field.title/}</a> 本文章网址: < ...
- innerHtml写法
swt_center = "<div id='new_swt_wee'>"; swt_center += '<a href="javascript:vo ...
- MySql数据库1【概念】
[mysql] mysql是目前最主流的跨平台.开放源代码的关系型数据库,由瑞曲的mysql ab公司开发,已经被SUN公司收购,标识是一只名为sakila的海豚,代表mysql的速度.能力.精确优秀 ...