【LeetCode】 sort list 单清单归并
称号:Sort a linked list in O(n log n) time using constant space complexity.
思路:要求时间复杂度O(nlogn)
知识点:归并排序,链表找到中点的方法
存在的缺点:边界条件多考虑!。!
/**
* LeetCode Sort List Sort a linked list in O(n log n) time using constant space complexity.
* 题目:将一个单链表进行排序,时间复杂度要求为o(nlogn)
* 思路:1时间复杂度为o(nlog n)的排序算法有:归并排序、快排(期望)、堆排序
* 2、单链表排序用归并排序。双链表排序用快排
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ package javaTrain; class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Train4 {
public ListNode sortList(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode fast = head;
ListNode slow = head;
while(fast.next.next != null && slow.next != null){
fast = fast.next.next; //使得当遍历完该链表之后一个指向中间一个指向末尾,即找到链表中点
slow = slow.next;
}
ListNode list2 = slow.next;
slow.next = null;
head = sortList(head);
list2 = sortList(list2);
return merge(head,list2);
}
private static ListNode merge(ListNode list1,ListNode list2){
if(list1 == null) return list2;
if(list2 == null) return list1;
ListNode head = new ListNode(0);
ListNode last = head;
while(list1.next != null && list2.next != null){
if(list1.val <= list2.val){
last.next = list1;
list1 = list1.next;
}
else{
last.next = list2;
list2 = list2.next;
}
last = last.next;
}
if(list1 != null)
last.next = list1;
else if(list2 != null)
last.next = list2;
return head.next;
} }
版权声明:本文博主原创文章。博客,未经同意不得转载。
【LeetCode】 sort list 单清单归并的更多相关文章
- LeetCode—-Sort List
LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...
- LeetCode Sort List 链表排序(规定 O(nlogn) )
Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...
- [LeetCode] Course Schedule 课程清单
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode sort List
Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-l ...
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- [leetcode]Sort List @ Python
原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...
随机推荐
- ORACLE—002:Create创作型
--用于工作的积累SQL ORACLE另外还有的类型.储过程.函数等的输入输入出. 以下看下创建. 使用方法 CREATE OR REPLACE TYPE 类型名称 AS OBJECT( 字段1 ...
- hdu2852--KiKi's K-Number(段树,求第一k的数量)
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Jvascript方法
Jvascript实用方法 这篇我主要记录一些在工作中常用的.实用的方法. String trim 字符串方法中的trim主要用来去空格使用,很多时候,在后台做参数处理的时候,我们都会使用该方法, ...
- 【转】MAT(Memory Analyzer Tool)工具入门介绍
1.MAT是什么? MAT(Memory Analyzer Tool),一个基于Eclipse的内存分析工具,是一个快速.功能丰富的JAVA heap分析工具,它可以帮助我们查找内存泄漏和减少内存消耗 ...
- 古老server源代码迁移到新server
因为老vsts资源server不久,准备存档,现在在旧的需要server该代码仍然在使用的所有迁移到新的vstsserver在. 因此,我们需要迁移所有需要也许是习惯了新的代码vsts在之上.代码的迁 ...
- UML造型——使用EA时序图工具的开发实践和经验
Enterprise Architect watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb3l3NzE=/font/5a6L5L2T/fontsiz ...
- HDU 3832 Earth Hour(最短路)
题目地址:HDU 3832 这个题的这种方法我无法给出证明. 我当时这个灵感出来的时候是想的是要想覆盖的点最少,那就要尽量反复利用这些点,然后要有两个之间是通过还有一个点间接连接的,这样会充分利用那些 ...
- jqm的多列布局demo,html5的多列布局demo,多列布局的具体解说,html5开发实例具体解释
因为移动设备屏幕宽度较小,所以一般不建议使用多列布局.但有时你可能须要并排放置一些元素(如button之类的). jQuery Mobile通过约定的类名ui-grid来提供了一种基于css的多列布局 ...
- 【C++基金会 04】vector详细解释
根据写博客开始总有一些事情的习惯,加鸡汤文,今天请原谅我记得. ============================================= 今天要写的内容是顺序型容器.首先,标准库定义 ...
- QlikView线图高亮选择尺寸
作为标题,如今,学生问我一个问题.尺寸Month.expression它是Count(Id). 这个图是一个折线图,不管你选择哪个月的其他下拉列表,销售量.由于Expression里面是这样写的 Co ...