45.Sort List(链表排序)
Level:
Medium
题目描述:
Sort a linked list in O(n log n) time using constant space complexity.
Example 1:
Input: 4->2->1->3
Output: 1->2->3->4
Example 2:
Input: -1->5->3->4->0
Output: -1->0->3->4->5
思路分析:
对链表进行归并排序,时间复杂为O(nlgn),空间复杂度为O(1)。
代码:
public class Solution{
public ListNode sortList(ListNode head){
if(head==null||head.next=null)
return head;
ListNode slow=head;
ListNode fast=head;
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
}
ListNode right=slow.next;
slow.next=null;//将左右链表断开
return merge(sortList(head),sortList(right));
}
public ListNode merge(ListNode head,ListNode right){
if(head==null)
return right;
if(right==null)
return head;
ListNode pHead=new ListNode(0);
if(head.val<right.val){
pHead=head;
pHead.next=merge(head.next,right);
}else{
pHead=right;
pHead.next=merge(head,right.next);
}
return pHead;
}
}
45.Sort List(链表排序)的更多相关文章
- [LeetCode] Sort List 链表排序
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] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- [LintCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- sort 树 hash 排序
STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
- 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...
链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...
- C语言 链表的使用(链表的增删查改,链表逆转,链表排序)
//链表的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include< ...
- c语言:链表排序, 链表反转
下面将实现链表排序的排序和遍历显示功能: 所定义的链表结构如下: head -> p1 -> p2 ->p3 ->....->pn; head的本身不作为数据节点,hea ...
随机推荐
- 【记录】eclipse jar包看不了源码
第一步:下载JAD . jad官方地址的官方下载地址是: http://www.softpedia.com/get/Programming/Debuggers-Decompilers-Dissasem ...
- mysql02---客户端与服务器模型
目录 一.客户端与服务器模型 连接MySQL方式 总结: 二.MySQL服务器构成 三.MySQL的结构 一.客户端与服务器模型 1.mysql是一个典型的C/S服务结构 1.1 mysql自带的客户 ...
- axios之前端发送get与post请求模板
import axios from "axios"; 一.get mounted() { axios .get( "/api/queryusertree?domId=&q ...
- day17 python re模块 简易爬虫
day17 python 一.re模块 1.re模块的基础方法 查找findall() import re #re.findall(pattern,string,flags ...
- SpringIntegration---Redis
1.依赖 <dependency> <groupId>org.springframework.integration</groupId> <artifactI ...
- 51nod 1298:圆与三角形(计算几何)
题目链接 判断圆和三角形是否相交 可以转化为 判断三条线段是否和圆相交 #include<iostream> #include<cstdio> #include< ...
- 如何在pycharm中进入shell脚本调试代码
首先在Teramal终端 输入python manage.py shell 然后进行下图操作来调试代码
- CGContextRef 使用小记
. 用CGContextRef 画文字 在 UIView的 - (void)drawRect:(CGRect)rect {} 方法中进行 CGContextRef context = UIGraphi ...
- 十条服务器端优化Web性能的技巧
服务器 远程桌面连接工具 提高web应用的性能从来没有比现在更重要过.网络经济的比重一直在增长;全球经济超过5%的价值是在因特网上产生的(数据参见下面的资料).这个时刻在线的超连接世界意味着用户对其 ...
- bzoj 2002[Hnoi2010]Bounce 弹飞绵羊(分治分块)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...