Leetcode148-Sort_List
Sort_List
在LeetCode 里面,因为只有归并排序的时间复杂度为O(1),所以快速排序用不了,前面两个都没用直接看最后一个归并排序。
冒泡排序(超时了)
public ListNode sortList(ListNode head) {
if(null == head)
return head;
int counter = 0;
ListNode current = head;
while (null != current.next) {
current = current.next;
counter++;
}
current = head;
int pre;
while (counter > 0) {
for (int i = 0; i < counter; i++) {
if (current.val > current.next.val) {
pre = current.val;
current.val = current.next.val;
current.next.val = pre;
}
current = current.next;
}
current = head;
counter--;
}
return head;
}
快速排序(在leetcode 上面还是超时间了)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
quickSort(head, null);
return head; }
public static ListNode quickSort(ListNode head, ListNode end) {
if (head != end) { ListNode p1 = head;
ListNode p2 = head.next; //走到末尾才停
while (p2 != null) { //大于key值时,p1向前走一步,交换p1与p2的值
if ((p2.val < head.val) && (p1 != p2)) {
p1 = p1.next;
int temp = p1.val;
p1.val = p2.val;
p2.val = temp;
}
p2 = p2.next;
} //当有序时,不交换p1和key值
if (p1 != head) {
int temp = p1.val;
p1.val = head.val;
head.val = temp;
}
quickSort(head, p1);
quickSort(p1.next, null);
return p1; }
return head;
}
}
归并排序
思路:https://blog.csdn.net/mine_song/article/details/69831827
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) { if (null == head || null == head.next) {
return head;
}
ListNode mid = getMidNode(head);
ListNode right = mid.next;
mid.next = null;
return mergeSort(sortList(head), sortList(right)); } static ListNode getMidNode(ListNode node) { ListNode fast = node;
ListNode slow = node; while (null != fast.next && null != fast.next.next) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
} static ListNode mergeSort(ListNode head1, ListNode head2) { ListNode p1 = head1;
ListNode p2 = head2;
ListNode head;
if (p1.val < p2.val) {
head = p1;
p1 = p1.next;
} else {
head = p2;
p2 = p2.next;
}
ListNode p = head;
while (null != p1 && null != p2) {
if (p1.val <= p2.val) {
p.next = p1;
p1 = p1.next;
p = p.next;
} else {
p.next = p2;
p2 = p2.next;
p = p.next;
}
} if (null != p1) {
p.next = p1;
}
if (null != p2) {
p.next = p2;
}
return head;
}
}
Leetcode148-Sort_List的更多相关文章
- 【LeetCode148】Sort List★★bug
1.题目描述: 2.解题思路: 本题是要堆一个链表进行排序,并且要求时间复杂度为 O(n log n).很明显,要用到分治的思想,用二分法进行归并排序:找到链表的middle节点,然后递归对前半部分和 ...
- [Swift]LeetCode148. 排序链表 | Sort List
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- leetcode148. Sort List
和数组里面的归并排序相同,用两个指针分别对应low high,递归进行归并排序然后merge把两个链表合在一起 /** * Definition for singly-linked list. * s ...
- leetcode148
class Solution { public: ListNode* sortList(ListNode* head) { multimap<int,ListNode*> mul; whi ...
- LeetCode148:Sort List
题目: Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 根据题目要求,可知只能用归并排序,其他 ...
- 【leetcode-148】排序链表
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3输出: 1->2->3->4示例 2: 输入: ...
- Leetcode148. Sort List排序链表
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3 输出: 1->2->3->4 示例 2: 输入 ...
- python之在线PK游戏(第六天)
本节作业: 熟练使用类和模块,写一个交互性强.有冲突的程序. 故本次写了一个文字回合制的PK游戏,系统主程序为根目录下的:game_menu.py 1. 系统功能模块: 第六天的作业:文字游戏程序 ...
- oracle 队列
Oracle 高级队列(AQ) 适用对象:初步了解oracle高级队列人群 注意事项: 序号 注意事项 1 JMS监听部分可参考官方文档: http://docs.oracle.com/cd/e128 ...
- Linux IO Scheduler(Linux IO 调度器)
每个块设备或者块设备的分区,都对应有自身的请求队列(request_queue),而每个请求队列都可以选择一个I/O调度器来协调所递交的request.I/O调度器的基本目的是将请求按照它们对应在块设 ...
随机推荐
- go tcp通信
----tcp 客户端 package main import ( "net" "fmt" ) func main() { conn,err := net.Di ...
- java - mybatis:java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for
当遇见java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for 错误的时候 ...
- ubuntu python 版本管理
ubuntu 命令行查看 python 目录 $ whereis python # 显示所有得到 python 目录 $ which python # 显示默认的 python 解释器目录 $ wh ...
- python asyncio 获取协程返回值和使用callback
1. 获取协程返回值,实质就是future中的task import asyncioimport timeasync def get_html(url): print("start get ...
- SimdJsonSharp:每秒解析千兆字节的JSON
SimdJsonSharp: Parsing gigabytes of JSON per second C# version of lemire/simdjson (by Daniel Lemire ...
- RESTful Webservice 和 SOAP Webserivce 对比及区别【转】
接口抽象 RESTful Web 服务使用标准的 HTTP 方法 (GET/PUT/POST/DELETE) 来抽象所有 Web 系统的服务能力,而不同的是,SOAP 应用都通过定义自己个性化的接口方 ...
- LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination
题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...
- ASP.NET Core 3.0 使用AspectCore-Framework实现AOP
AspectCore是适用于Asp.Net Core 平台的轻量级Aop(Aspect-oriented programming)解决方案,它更好的遵循Asp.Net Core的模块化开发理念,使用A ...
- Git以及GitHub的一些基本使用
1:注册GitHub账号: https://github.com/ 2:Git bash工具下载地址 https://gitforwindows.org/ 3:怎么在GitHub 新增 SSH Key ...
- python 处理中文遇到的编码问题总结 以及 字符str的编码如何判断
如何处理中午编码的问题 Python的UnicodeDecodeError: 'utf8' codec can't decode byte 0xxx in position 这个错误是因为你代码中的某 ...