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)的时间复杂度和 ...
随机推荐
- Java实现文件下载
一.html <button class="ui-btn ui-btn-primary left20" onclick="downloadXlsTemplate() ...
- js不记录某个url链接历史访问,返回时不返回该链接
(function(){ var fnUrlReplace = function (eleLink) { if (!eleLink) { return; } var href = eleLink.hr ...
- linux centos系统 php安装GD库扩展
yum --enablerepo=remi-php56 install php-gd php-mysql php-mbstring php-xml php-mcrypt //安装GD库扩展 servi ...
- Docker系列(10)- 常用命令小结
#橙色前面笔记已记录,黑色后面笔记将完善#勤加练习!!!attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像 build ...
- P5666-[CSP-S2019]树的重心【树状数组】
正题 题目链接:https://www.luogu.com.cn/problem/P5666 题目大意 给出\(n\)个点的一棵树,对于每条边割掉后两棵树重心编号和. \(1\leq T\leq 5, ...
- EasyExcel无法用转换器或者注解将java字段写入为excel的数值格式
需求: 在用easyExcel导出报表时,碰到需要将数据转换为数值or货币格式的需求 过程: 1.首先采取转换器的形式 @Override public CellData convertToExcel ...
- Sentry 监控 - Alerts 告警
系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...
- DBeaver MSSQL 支持TLS设置
DBeaver是一个基于 Java 开发,免费开源的通用数据库管理和开发工具,使用非常友好的 ASL 协议.可以通过官方网站或者 Github 进行下载. 由于 DBeaver 基于 Java 开发, ...
- Linux环境yum,安装MySQL
Linux 使用yum命令安装mysql [安装步骤] 1.先检查系统是否安装有mysql [root@localhost ~]#yum list installed mysql* [root@loc ...
- ch_nginx.sh
#!/bin/bash counter=`ps -ef |grep nginx |grep -v grep | wc -l` if [ $counter = 0 ];then service ngin ...