【刷题-LeetCode】148 Sort List
- Sort List
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
解法1 归并排序。用两个函数实现:
- merge:将两个有序链表合在一起
- merge_sort:将无序链表排序
找中间位置用双指针
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head) {
if(head == NULL || head->next == NULL)return head;
ListNode *mid, *pre;
find_mid(head, mid, pre);
pre->next = NULL;
return merge(sortList(head), sortList(mid));
}
// 找中点时,需要对mid和pre做修改,因此需要传引用
void find_mid(ListNode* s, ListNode* &mid, ListNode* &pre){
ListNode *pp = s;
mid = s;
while(pp && pp->next){
pre = mid;
mid = mid->next;
pp = pp->next->next;
}
}
ListNode* merge(ListNode* s1, ListNode* s2){
ListNode *head = new ListNode, *p = s1, *q = s2;
ListNode *cur = head;
while(p != NULL && q != NULL){
if(p->val < q->val){
cur->next = p;
p = p->next;
}else{
cur->next = q;
q = q->next;
}
cur = cur->next;
}
if(p != NULL)cur->next = p;
else cur->next = q;
return head->next;
}
};
解法2 快速排序。partition过程中,可以用两个链表small和large分别存储pivot左侧和右侧的数据
class Solution {
public:
ListNode* sortList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* cur = head->next;
ListNode* small = new ListNode(0);
ListNode* large = new ListNode(0);
ListNode* sp = small;
ListNode* lp = large;
// partition
while(cur){
if(cur->val<head->val){
sp->next = cur;
sp = cur;
}
else{
lp->next = cur;
lp = cur;
}
cur = cur->next;
}
sp->next = NULL;
lp->next = NULL;
small=sortList(small->next);
large=sortList(large->next);
cur = small;
if(cur){
while(cur->next) cur = cur->next;
cur->next = head;
head->next = large;
return small;
}else{
head->next = large;
return head;
}
}
};
【刷题-LeetCode】148 Sort List的更多相关文章
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- LeetCode刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
- 【leetcode刷题笔记】Sort List
Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找 ...
- 【刷题-LeetCode】147 Insertion Sort List
Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- leetcode 148. Sort List ----- java
Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...
- Leetcode#148 Sort List
原题地址 链表归并排序 真是恶心的一道题啊,哇了好多次才过. 代码: void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNo ...
随机推荐
- UVA10976 分数拆分 Fractions Again?! 题解
Content 给定正整数 \(k\),找到所有的正整数 \(x \geqslant y\),使得 \(\frac{1}{k}=\frac{1}{x}+\frac{1}{y}\). 数据范围:\(0& ...
- LuoguP6857 梦中梦与不再有梦 题解
Update \(\texttt{2020.10.20}\) 增加了证明.感谢@东北小蟹蟹(dbxxxqwq)的提醒. Content 有一个 \(n\) 个点的无向图,每两个点之间都有一条边直接相连 ...
- LuoguP7019 [NWRRC2017]Auxiliary Project 题解
Update \(\texttt{2021.6.24}\) 修改了一处格式上的错误和一处笔误. Content 已知用 LED 灯来显示 \(0\sim9\) 这十个数字分别需要 \(6,2,5,5, ...
- Qt Creator 源码学习笔记04,多插件实现原理分析
阅读本文大概需要 8 分钟 插件听上去很高大上,实际上就是一个个动态库,动态库在不同平台下后缀名不一样,比如在 Windows下以.dll结尾,Linux 下以.so结尾 开发插件其实就是开发一个动态 ...
- 两阶提交、三阶提交、TCC框架
首先介绍一下分布式事务,分布式事务是指会涉及到操作多个数据库的事务.其实就是将对同一库事务的概念扩大到了对多个库的事务.目的是为了保证分布式系统中的数据一致性.分布式事务处理的关键是必须有一种方法可以 ...
- python 银行管理系统
这是一个使用python连接mysql的例子 涉及到类的使用 import pymysql import function as f def mysql(): db=pymysql.connect(h ...
- Linux使用docker安装RabbitMQ
拉取镜像 docker pull rabbitmq:management 启动容器 docker run -d --name rabbitmq -e RABBITMQ_DEFAULT_USER=adm ...
- protoc格式生成java文件
下载protoc.exe 地址:https://yvioo.lanzoui.com/i12opqs7q9g 下载好之后 ,把protoc文件和exe放在一个文件夹内 用记事本打开protoc,删掉包路 ...
- 【剑指Offer】二叉树的镜像 解题报告(Python)
[剑指Offer]二叉树的镜像 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interviews 题 ...
- Oracle VirtualBox 配置宿主机与虚拟机互访
写作原因 之前经常使用 VMware Workstation Pro,由于是非正常付费使用的不打算再用了.替代品就是 Oracle VirtualBox,但是发现了一个问题: "我想用NAT ...