称号: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. Android - 和其他APP交互 - 让其他app启动你的activity

    前面的两篇文章主要讲了一个方面:从app中启动其他app.但是如果你的app可以处理对其他app有用的操作,你的app也应该响应其他app的操作请求.例如,如果你创建了一个社交app可以分享信息和图片 ...

  2. 【C语言探索之旅】 第三课:你的第一个程序

    内容简介 1.课程大纲 2.第一部分第三课:你的第一个程序 3.第一部分第四课预告:变量的世界 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个 ...

  3. Html5用Canvas制作画图板

    需求: 绘制多边形 可填充颜色 可设置文字 可移动,可删除 鼠标按住后,抬起之前线段应该尾随鼠标当前位置 可与后台方便的进行数据交互,保存到后台,或将数据从后台取到前台显示对应的图形 思考: 第一想到 ...

  4. UVA 1358 - Generator(dp+高斯消元+KMP)

    UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...

  5. Android提高第二篇之SurfaceView的基本使用

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 上次介绍MediaPlayer的时候略微介绍了SurfaceView,SurfaceView因为能够直接从内存 ...

  6. 4.帧循环(游戏循环),schedule

     1 概述 游戏乃至图形界面的本质是不断地画图,然而画图并非任意的,不论什么游戏都须要遵循一定的规则来呈现出来,这些规则就体现为游戏逻辑.游戏逻辑会控制游戏内容,使其依据用户输入和时间流逝而改变. ...

  7. 如何设置eclipse在默认模式下打开文件

    如何设置eclipse在默认模式下打开文件 打开eclipse.选择例如以下:windows --> preferences --> General --> Editors --&g ...

  8. Java之旅(三)--- JSTL和EL表情

     先给大家看一段JSP的代码.看看有什么感受? <% List<UsEL> usELList = pageModel.getList(); for (ItELator<Us ...

  9. hdu4770:Lights Against Dudely(回溯 + 修剪)

    称号:hdu4770:Lights Against Dudely 题目大意:相同是n*m的矩阵代表room,房间相同也有脆弱和牢固之分,如今要求要保护脆弱的房间.须要将每一个脆弱的房间都照亮,可是牢固 ...

  10. WAP页面点击与hover延迟解决之道

    最近一直在WAP端页面的开发,一直都知道wap端点击相关问题存在延迟.之前做的网页大部分使用a链接进行,一直未入此坑. 最近做的一个WAP网站,各种点击,hover事件,如果使用PC端网页的做法,直接 ...