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调度器的基本目的是将请求按照它们对应在块设 ...
随机推荐
- POJ3662Telephone Lines(最短路+二分)
传送门 题目大意:n个点p条边,每条边有权值,让1和n点联通,可以将联通1--n的边选k条免费, 求剩下边权的最大值. 题解:二分一个答案x,大于x的边权设为1,小于等于x的边权设为0,跑最短路. 若 ...
- glade No package 'libxml-2.0' found
------------恢复内容开始------------ 今天突发奇想 root@Aja:~/下载/libxml2-master# glade 不会用纳 百度一下——————————>> ...
- Paper | Making a "Completely Blind" Image Quality Analyzer
目录 1. 技术细节 1.1 NSS特征 1.2 选择锐利块来计算NSS 1.3 一张图像得到36个特征 1.4 用MVG建模这36个特征 1.5 NIQE指标 2. 实验 质量评估大佬AC Bovi ...
- c# 多线程 双色球
学习记录.仅供参考. 知识点: 多线程 Lock 环境: Visual Studio 2017 public partial class Form1 : Form { private static r ...
- iOS Workflow 分享 - Debug Action
有时候我们想要知道别人的 app 在调用 Share Extension 时提供了什么类型的数据以及具体数据是什么,我们可以自己在 Xcode 里面写个 app 去接收别人 app 的数据,但我们也可 ...
- 大话设计模式Python实现-外观模式
外观模式(Facade Pattern):为子系统中的一组接口提供一个一致界面,此模式定义一个高层接口,使得子系统更加容易使用 下面是一个外观模式的demo: #!/usr/bin/env pytho ...
- MongoDB创建数据库和删除数据库05-14学习笔记
MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写,是一个基于分布式文件存储的开源数据库系统.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关 ...
- Module Code: CMT107
Cardiff School of Computer Science and InformaticsCoursework Assessment Pro-formaModule Code: CMT107 ...
- 如何在yii1.0.7中设置数据库连接超时?
继承CDbConnection, 覆盖 init()方法 在 parent::init() 之前设置 $this->setAttribute(PDO::ATTR_TIMEOUT, $this-& ...
- Kafka 2.3 Producer (0.9以后版本适用)
kafka0.9版本以后用java重新编写了producer,废除了原来scala编写的版本. 这里直接使用最新2.3版本,0.9以后的版本都适用. 注意引用的包为:org.apache.kafka. ...