【刷题-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 ...
随机推荐
- ASP.NET MVC 导入Excel文件(完整版)
View视图部分: <form method="post" enctype="multipart/form-data" action="/Pos ...
- java 数据类型:ArrayList;LinkList性能分析
各种线性表的性能分析. java提供的List就是一个线性表接口,ArrayList和LinkedList是线性表的两种实现.基于数组的线性表和基于链表的线性表. 一般来说,我们无需理会ArrayLi ...
- ACwing1216. 饮料换购
题目: 乐羊羊饮料厂正在举办一次促销优惠活动.乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料,并且可以一直循环下去(但不允许暂借或赊账). 请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么,对于 ...
- 往docker中的mysql导入sql文件
先把sql文件上传到服务器 然后拷贝sql文件到docker中的mysql 容器中 docker cp test.sql mysql:/test.sql 这里的mysql都是容器名称 根据自己的来 用 ...
- mysql报错:You do not have the SUPER privilege and binary logging is enabled
MySQL出现 You do not have the SUPER privilege and binary logging is enabled报错 解决方案: 1.用root用户登录:mysql ...
- std::bind与std::ref, why and how
首先解释下为什么有时候需要bind. 我们可以用bind从函数T add(T a, T b)造出个inc()来,即把b写死为1.这个例子本身比较傻,但有不傻的应用. template<typen ...
- (gcd(a,b) == axorb)==>b = a-gcd(a,b)
\((gcd(a,b) == axorb)==>b = a-gcd(a,b)\)的证明 \[(gcd(a,b) == axorb)==>b = a-gcd(a,b) \] 证明$$a-b& ...
- 【剑指Offer】链表中环的入口结点 解题报告(Python)
[剑指Offer]链表中环的入口结点 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...
- TensorFlow.NET机器学习入门【4】采用神经网络处理分类问题
上一篇文章我们介绍了通过神经网络来处理一个非线性回归的问题,这次我们将采用神经网络来处理一个多元分类的问题. 这次我们解决这样一个问题:输入一个人的身高和体重的数据,程序判断出这个人的身材状况,一共三 ...
- [源码解析] PyTorch 分布式之弹性训练(5)---Rendezvous 引擎
[源码解析] PyTorch 分布式之弹性训练(5)---Rendezvous 引擎 目录 [源码解析] PyTorch 分布式之弹性训练(5)---Rendezvous 引擎 0x00 摘要 0x0 ...