【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). 解题思路:由于题目对时间复杂度 ...
随机推荐
- web报告工具FineReport在使用方法和解决方案常见错误遇到(一)
FineReport在使用方法和解决方案常见错误遇到(一) 这里写的开胃菜.我希望我们能理清自己的问题和解决办法干出来的,Mark一点点.有利于所有. 失败搜索出,如果有一个文件,看看你的度娘那里.看 ...
- C#的WebBrowser控制浏览
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- VS2010使整个过程说明了安装包
该项目的第一个版本出来,要成为一个包,很长一段时间没做了一些被遗忘,上差了差资料,写了一个,总结下,可能还不是非常完好,仅作參考. 1.首先在打开 VS2010 >新建>项目 2.创 ...
- Redis数据结构
Redis数据结构 Redis数据结构详解(一) 前言 Redis和Memcached最大的区别,Redis 除啦支持数据持久化之外,还支持更多的数据类型而不仅仅是简单key-value结构的数据 ...
- 解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 问题(转)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-11: ordinal not in range(128 ...
- MariaDb数据库管理系统的学习(一)安装示意图
MariaDB数据库管理系统是MySQL的一个分支.主要由开源社区在维护,採用GPL授权许可.开发这个分支的原因之中的一个是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区採用分 ...
- JavaScript高级编程
原文地址: http://www.onlamp.com/pub/a/onlamp/2007/07/05/writing-advanced-javascript.html Web应用程 ...
- 策略模式设计模式(Strategy)摘录
23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例.一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将 ...
- hdu 1159 Common Subsequence (dp乞讨LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- php 简单的存在 (方法之间的神奇作用:容错)
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?php // 重载 class Person{ ...