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 ...
随机推荐
- MySQL和时间戳有关的函数
1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()mysql> select current_timestamp, current ...
- react 组件积累
material-ui material-table ant-design https://ant.design/docs/react/getting-started-cn 定义组件(注意,组件的名称 ...
- 转载:futex同步机制详解
在编译2.6内核的时候,你会在编译选项中看到[*] Enable futex support这一项,上网查,有的资料会告诉你"不选这个内核不一定能正确的运行使用glibc的程序", ...
- PCI 设备详解三
上篇文章已经分析了探测PCI总线的部分代码,碍于篇幅,这里另启一篇.重点分析下pci_scan_root_bus函数 2016-10-24 pci_scan_root_bus函数 struct pci ...
- Win2003X64位,IIS6.0 32位 浏览报错的解决方案
目录 问题案例 原因分析 解决问题 其他 问题案例 1)服务浏览出现: service unavailable 2)服务浏览出现:HTTP 404 当前页找不到 3)在事件查看器:应用程序中报错:在同 ...
- SpringBean 定义继承
Bean定义继承 bean定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等.子bean的定义继承副定义的配置数据.子定义可以根据需要重写一些值 ...
- 单例Singleton模式的两种实现方法
在设计模式中,有一种叫Singleton模式的,用它可以实现一次只运行一个实例.就是说在程序运行期间,某个类只能有一个实例在运行.这种模式用途比较广泛,会经常用到,下面是Singleton模式的两种实 ...
- openfire 使用已有的数据库作为用户认证数据库 Custom Database Integration Guide
http://download.igniterealtime.org/openfire/docs/latest/documentation/db-integration-guide.html Intr ...
- windows平台tensorboard的配置及使用
由于官网和其他教程里面都是以Linux为平台演示tensorboard使用的,而在Windows上与Linux上会有一些差别,因此我将学习的过程记录下来与大家分享(基于tensorflow1.2.1版 ...
- 在MFC下面实际演示CCriticalSection 的使用
Q:CCriticalSection是什么? A:CCriticalSection是一种线程同步策略 或者说技术 或者方法 总之呢就是这么个意思.... 参考资料: http://blog.csdn ...