称号: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. DevExpress 12.1 换肤 超级简单的方法(2013-11-5版)

    本例子是按照DevExpress 12.1 版本 进行演示.请先准备好DevExpress.BonusSkins.v12.1.dll 和DevExpress.Utils.v12.1.dll 1.首先添 ...

  2. .NET 并行(多核)编程系列之七 共享数据问题和解决概述

    原文:.NET 并行(多核)编程系列之七 共享数据问题和解决概述 .NET 并行(多核)编程系列之七 共享数据问题和解决概述 前言:之前的文章介绍了了并行编程的一些基础的知识,从本篇开始,将会讲述并行 ...

  3. HDU 4513 哥几个系列故事——形成完善II manacher求最长回文

    标题来源:哥几个系列故事--形成完善II 意甲冠军:中国 思维:在manacher断 保证非严格递减即可了 #include <cstdio> #include <cstring&g ...

  4. 设计模式模式游客(Visitor)摘录

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将实例化托付给还 ...

  5. InputMonitor注意事项

    文章只记录自己的点点理解.为了你自己的参考. 1.mInputFocus WMS.addWindow()-->WMS.finishUpdateFocusedWindowAfterAssignLa ...

  6. Java拾遗(一):浅析Java子类和父类的实例化顺序 及 陷阱

    本文主要介绍Java里经常使用的子类和父类的变量实例化顺序及陷阱,并结合一个Android实例来探讨此问题.日后编程中应尽量避免此陷阱. 首先看以下一段代码: 定义一个虚类Server.java pa ...

  7. hdu2665-Kth number

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. Windows Phone 启动器

    http://msdn.microsoft.com/zh-CN/library/gg278408(v=vs.92)#BKMK_Launchers using Microsoft.Phone.Contr ...

  9. Tempdb怎么会成为性能瓶颈

    原文:Tempdb怎么会成为性能瓶颈 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/25/tempdb.aspx 我曾经遇到过这样一个性能问题. ...

  10. Appium - iOS Mac环境结构

    Appium - iOS Mac环境结构 笔者: Max.Bai 时间: 2014/10 1. iOS开发环境的搭建 1.1系统要求 MacOS X 10.7 or higher, 10.9.2 re ...