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调度器的基本目的是将请求按照它们对应在块设 ...
随机推荐
- C 函数与指针(function & pointer)
C 函数与指针(function & pointer) /* * function.c * 函数在C中的使用 * */ #include <stdio.h> int noswap( ...
- springcloud源码分析(一)之采用redis实现注册中心
注册中心 在分布式架构中注册中心起到了管理各种服务功能包括服务的注册.发现.熔断.负载.降级等功能,在分布式架构中起到了不可替代的作用.常见的注册中心有eureka,zookeeper等等,在spri ...
- Mac操作:Mac系统移动鼠标显示桌面(移动鼠标到角落)
很多朋友都发现,有的人在用Mac的时候,鼠标一划就可以显示桌面,或者显示Launchpad.其实很简单,下面就介绍这个方法. 首先打开系统偏好设置: 然后点击红色圈中的图标:MissionContro ...
- win7 下docker 镜像加速
打开 Kitematic 运行 docker cli 注册镜像 https://www.daocloud.io/mirror#accelerator-doc 上有镜像地址 sudo sed -i &q ...
- Java Metrics工具介绍
目录 简介 快速入门 Maven配置 MetricRegistry Gauge Meter Counter Histgram Timer Reporter 更多用法 参考资料 简介 Metric是一个 ...
- flink solt,并行度
转自:https://www.jianshu.com/p/3598f23031e6 简介 Flink运行时主要角色有两个:JobManager和TaskManager,无论是standalone集群, ...
- mysql 5 长度解析
mysql 5 以后 都按照字符来算 不是字节 char(10)可以放10个汉字或者10个字母
- 【前端开发环境】前端使用GIT管理代码仓库需要掌握的几个必备技巧和知识点总结
1. Git的三种状态 已提交 committed 已暂存 staged 已修改 modified 2. Git的三个区域 Git仓库 是 Git 用来保存项目的元数据和对象数据库的地方. 这是 Gi ...
- Reids Lua 模糊查询所有key 及 相对应的集合总数
Redis 使用 Lua 模糊查询所有key 及 相对应的集合总数 .Net 4.5.1 需要引入: StackExchange.Redis (测试用的 1.2.4.0) 方法一: 优点:原子 ...
- 如何让 FFmpeg 支持异步并行转码、截图等等操作?
直接贴代码了: ffmpegTest02.cs public partial class ffmpegTest02 : FormBase { private static readonly strin ...