LeetCode——Sort List
Question
Sort a linked list in O(n log n) time using constant space complexity.
Solution
分析,时间复杂度要求为nlogn,因此得考虑归并排序,但是空间必须为常量,因此得注意指针的操作。
Code
/**
* 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 == NULL)
return head;
if (head->next == NULL)
return head;
ListNode* p1 = head;
ListNode* p2 = head;
ListNode* pre = head;
// fast and slow 两个指针,用于找到中间节点
// pre节点,需要把链表断开,生成两个链表
while ( p2 != NULL && p2->next != NULL) {
pre = p1;
p1 = p1->next;
p2 = p2->next->next;
}
pre->next = NULL;
ListNode* l1 = sortList(head);
ListNode* l2 = sortList(p1);
return merge(l1, l2);
}
ListNode* merge(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1;
if (l1->val <= l2->val) {
l1->next = merge(l1->next, l2);
return l1; // 注意要从尾向前返回
}
else {
l2->next = merge(l1, l2->next);
return l2;
}
}
};
LeetCode——Sort List的更多相关文章
- LeetCode—-Sort List
LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- LeetCode Sort List 链表排序(规定 O(nlogn) )
Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...
- [LeetCode] Sort Transformed Array 变换数组排序
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- Leetcode: Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- leetcode sort List
Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-l ...
随机推荐
- flume jetty 进程关系 flume jetty 跨域问题 jetty 源码分析
flume jetty 跨域问题 13481 httpSource的端口进程号 = flume 启动后的进程号 [root@c log]# netstat -atp Active Internet ...
- Gartner提出的7种多租户模型
下面,我们就来看看在SaaS应用搭建过程中,可以采用什么样的多租户模型.从而能较为清晰地了解未来使用PaaS平台开发的SaaS,可以为用户提供哪些多租户的服务. Gartner提出了7种 ...
- 服务器端Session和客户端Session(和Cookie区别)2
https://blog.csdn.net/java_faep/article/details/78082802 我们可以得出如下结论: 关闭浏览器,只会是浏览器端内存里的session cookie ...
- SpringBoot 之数据访问
1. Spring Boot 与 JDBC 默认使用 org.apache.tomcat.jdbc.pool.DataSource 数据源; // application.yml spring: da ...
- SmokePing安装手册
SmokePing安装部署 SmokePing简介 Smokeping是一款用于网络性能监测的开源监控软件,主要用于对IDC的网络状况,网络质量,稳定性等做检测,通过rrdtool制图方式,图形化地展 ...
- django中实现微信消息推送
-公众号(不能主动给用户发消息) -认证的公众号:需要营业执照,需要交钱,可以发多篇文章 -未认证的公众号:一天只能发一篇文章 -服务号(微信推送) -需要申请,需要认证 -可以主动给用户推送消息 - ...
- Sping+ActiveMQ整合
通过前一篇<ActiveMQ简述>大概对ActiveMQ有了一个大概的认识.本篇所阐述的是怎样通过Spring继承ActiveMQ进而更有效.更灵活的运用ActiveMQ. Spring和 ...
- VS安装程序制作之MSI/EXE
MSI文件是Windows Installer的数据包,它实际上是一个数据库,包含安装一种产品所需要的信息和在很多安装情形下安装(和卸载)程序所需的指令和数据.MSI文件将程序的组成文件与功能关联起来 ...
- python练习题(持续更新中。。。。。)
1.检验注册用户是否合法:需要输入用户名,校验用户名是否被注册,如已注册,提示已经注册过,没注册就可以注册:用户名不能为空:用户名长度必须在6-13位之间:最多只能输入三次. users = ['aa ...
- Spark2.0机器学习系列之4:Logistic回归及Binary分类(二分问题)结果评估
参数设置 α: 梯度上升算法迭代时候权重更新公式中包含 α : http://blog.csdn.net/lu597203933/article/details/38468303 为了更好理解 α和 ...