sort-list leetcode C++
Sort a linked list in O(n log n) time using constant space complexity.
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head){
if (!head||!head->next) return head;
ListNode* slow = head;
ListNode* fast = head;
ListNode* pre = head;
while (fast && fast->next){
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
pre->next = NULL;
return merge(sortList(head),sortList(slow));
}
ListNode* merge(ListNode* left,ListNode* right){
ListNode *head;
if (left->val < right->val){
head = left;
left = left->next;
}else{
head = right;
right = right->next;
}
ListNode *cur = head;
while(left && right){
if (left->val < right->val){
cur->next = left;
left = left->next;
}else{
cur->next = right;
right = right->next;
}
cur = cur->next;
}
if (left) cur->next = left;
if (right) cur->next = right;
return head;
}
ListNode* merge2(ListNode*left, ListNode* right){
ListNode* head = new ListNode(-1);
ListNode* cur = head;
while(left && right){
if (left->val < right->val){
cur->next = left;
left = left->next;
}else{
cur->next = right;
right = right->next;
}
cur = cur->next;
}
if (left) cur->next = left;
if (right) cur->next = right;
return head->next;
}
};
sort-list leetcode C++的更多相关文章
- Sort List leetcode
这个题一开始本想用快速排序的,但是想了20分钟都没有头绪,难点在于快速排序的随机访问无法用链表实现,不过如果可以实现快速排序partition函数就可以了,但是这可能比较复杂,于是改用其他排序方法,上 ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- Sort Colors [LeetCode]
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Sort Colors —— LeetCode
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Sort List ——LeetCode
Sort a linked list in O(n log n) time using constant space complexity. 链表排序,要求时间复杂度O(nlgn),我写的归并排序. ...
- Insertion Sort List —— LeetCode
Sort a linked list using insertion sort. 题目大意:将一个单链表使用插入排序的方式排序. 解题思路:先新建一个头指针,然后重新构建一下这个单链表,每次从头找到第 ...
- sort vector - leetcode 新用法
179. Largest Number sort(num.begin(), num.end(), [](int a, int b){ return to_string(a)+to_string(b) ...
- Insertion Sort List Leetcode
Sort a linked list using insertion sort. 这个题我巧妙的设置了一个临时头结点 class Solution { public: ListNode* insert ...
- Sort Colors leetcode java
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- Sort List leetcode java
题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和 ...
随机推荐
- 安卓使用讯飞sdk报错
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.iflytek.cloud.SpeechSy ...
- PHP中的文件系统函数(一)
从这篇文章开始,我们将学习一系列的 PHP 文件系统相关函数.其实这些函数中,有很多都是我们经常用到的,大家并不需要刻意地去记住它们,只要知道有这么个东西,在使用的时候记得来查文档就可以了. 文件路径 ...
- google插件网页播放mp4代码
<script src="http://html5media.googlecode.com/svn/trunk/src/html5media.min.js"></ ...
- js特效代码-onmouseover/onclick 改变标签(背景)颜色
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- PHP 流行的框架
Aura Laravel Symphony Yii Zend php components Packagist 最好的组件: Awesome PHP https://www.yiiframework. ...
- P5110-块速递推【特征方程,分块】
正题 题目链接:https://www.luogu.com.cn/problem/P5110 题目大意 数列\(a\)满足 \[a_n=233a_{n-1}+666a_{n-2},a_0=0,a_1= ...
- JVM探针与字节码技术
JVM探针是自jdk1.5以来,由虚拟机提供的一套监控类加载器和符合虚拟机规范的代理接口,结合字节码指令能够让开发者实现无侵入的监控功能.如:监控生产环境中的函数调用情况或动态增加日志输出等等.虽然在 ...
- 2020.3.21--ICPC训练联盟周赛Benelux Algorithm Programming Contest 2019
A Appeal to the Audience 要想使得总和最大,就要使最大值被计算的次数最多.要想某个数被计算的多,就要使得它经过尽量多的节点.于是我们的目标就是找到 k 条从长到短的链,这些链互 ...
- JavaScript 数组 常用方法(二)
写在前面:续接上篇 JavaScript 数组 常用方法 数组常用方法第二弹来了: some && every 描述: every()与some()方法都是JS中数组的迭代方法. so ...
- python中常用的导包的方法和常用的库
python中常用的导包的方法 导入包和包名的方法:1.import package.module 2.from package.module import * 例一: ...