称号: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 单清单归并的更多相关文章

  1. LeetCode—-Sort List

    LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...

  2. LeetCode Sort List 链表排序(规定 O(nlogn) )

    Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...

  3. [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 ...

  4. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  5. [LeetCode] Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  6. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. leetcode sort List

    Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-l ...

  8. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  9. [leetcode]Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...

随机推荐

  1. 什么是PV,UV。

    PV浏览(Page View).该网页访问量,每次页面打开PV统计+1,也刷新. IP接入号码指独立IP接入号码,计算基于独立IP在计算的时间段来计算访问我们的网站1二级IP接入号码. 是否这个计算在 ...

  2. JavaFX横幅类游戏开发 教训 游戏贴图

    上一节课,我们即将完成战旗Demo有了一个大概的了解.教训这,我们将学习绘制游戏地图. 由于JavaFX 2.2中添加了Canvas相关的功能,我们就能够使用Canvas来实现游戏绘制了. 游戏地图绘 ...

  3. GitLab版本管理(转)

    GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目.它拥有与Github类似的功能,能够浏览源代码,管理 ...

  4. ajax jsonp跨域

    js跨域问题是指:js不同域进行数据传输或通信之间,让我们用ajax到不同的域请求数据.或js获得在不同领域的框架页(iframe)数据.只有到协议.域名.port无论是有不同的.它们被认为是不同的域 ...

  5. 第一篇——第一文 SQL Server 备份基础

    原文:第一篇--第一文 SQL Server 备份基础 当看这篇文章之前,请先给你的所有重要的库做一次完整数据库备份.下面正式开始备份还原的旅程. 原文出处: http://blog.csdn.net ...

  6. 初步boost之pool图书馆学习笔记

    pool 内存池概述 通常我们习惯直接使用new.malloc等API申请分配内存,这样做的缺点在于:因为所申请内存块的大小不定.当频繁使用时会造成大量的内存碎片并进而减少性能. 内存池则是在真正使用 ...

  7. 首先看K一个难看的数字

    把仅仅包括质因子2.3和5的数称作丑数(Ugly Number),比如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数. 写一个高效算法,返回第n个丑数. impor ...

  8. Maven工程引入jar包(转)

    Maven项目引入jar包的方法,希望能帮助有需要的朋友们 法一.手动导入:项目右键—>Build Path—>Configure Build Path—>选中Libraries—& ...

  9. ANDROID嵌入式应用Unity3D视图(画廊3D模型)

    转载请注明来自大型玉米的博客文章(http://blog.csdn.net/a396901990),谢谢支持! 效果展示:   watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  10. JavaScript之包装对象

    JavaScript对象是一种复合值:它是属性和已命名值的集合.通过"."符号来引用属性值.当属性值是一个函数时,称为方法. ①一段你常用但却未必明白其真正底层原理的代码: var ...