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调度器的基本目的是将请求按照它们对应在块设 ...
随机推荐
- javaScript___计算时间前一天和后一天案例
1. HTML 排版 <button onclick="anteayer()">前天</button> <button onclick=" ...
- Unity 插件宝典 (张忠喜 廖一庭 著)
第1章 模型类插件 第2章 特效类插件 第3章 动画插件 第4章 编辑器插件 第5章 脚本类插件 第6章 GUI插件 第7章 Shaders插件 第8章 优化类插件 第9章 综合应用----卡通版赛车 ...
- 改善java程序的151个建议
<编写高质量代码-改善java程序的151个建议> --秦小波 第一章.开发中通用的方法和准则 1.不要在常量和变量中出现易混淆的字母 long a=0l; --> long a=0 ...
- VS Code 提示‘未找到Git。请安装Git,或在“git.path”设置中配置’
一.情况说明 1.描述 从Git上克隆出代码,用vscode打开项目提示“未找到Git.请安装Git,或在“git.path”设置中配置” 2.截图 二.报错原因 .没有安装Git .没有设置Git路 ...
- mysql-新增数据表
新增数据表之前,需确保已经存在数据库,如还没有数据库请先参考上一篇文章新增数据库 1.创建表 create table test( id int PRIMARY KEY, name varcha ...
- CMPT 300 – Operating Systems
Assignment 4 – Create Simple YetFunctional File SystemCMPT 300 – Operating SystemsPlease submit a zi ...
- 使用OpenCL提升OpenCV图像处理性能 | speed up opencv image processing with OpenCL
本文首发于个人博客https://kezunlin.me/post/59afd8b3/,欢迎阅读最新内容! speed up opencv image processing with OpenCL G ...
- Python Jupyter 网站编辑器
Python Jupyter 网站编辑器 jupyter 是 python的网站编辑器可以直接在网页内编写python代码并执行,内置是通过ipython来调用的.很方便灵活. 安装 1.安装ipyt ...
- ssl与ssh
openssl genrsa -out private_key.pem 1024 ssh-keygen -t rsa -C zzf073@163.com ssl是安全会话协商机制: ssh是安全访问机 ...
- 1. mvc 树形控件tree + 表格jqgrid 显示界面
1.界面显示效果 2.资源下载 地址 1. jstree https://www.jstree.com/ 2.表格jqgrid https://blog.mn886.net/jqGrid/ ...