【原创】leetCodeOj --- Sort List 解题报告
今日leetcode链表题全制霸
原题地址:
https://oj.leetcode.com/problems/sort-list/
题目内容:
Sort List
Sort a linked list in O(n log n) time using constant space complexity.
方法:
题目要求是链表排序,同时时间复杂度要求O(n log n),空间复杂度要求常数空间。这意味着你不可以把链表拷贝到数组中,用一个map保留值和指针的对应关系,最后在构造一个链表。
我们需要考察不同的排序算法,看看哪种排序算法可以处理链表。关键在于哪种排序算法对于随机访问的依赖度低。
快排:首位指针,排除
堆排:要有两个儿子,排除
那就只能用归并排序了。
这次先给代码再分析复杂度
全部代码:
/**
* 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)
return NULL;
int len = countLength(head);
if (len == )
return head;
int mid = len / ;
ListNode *sec = dividedLst(head,mid);
head = sortList(head);
sec = sortList(sec);
head = mergeList(head,sec);
return head;
} ListNode *mergeList(ListNode *lst1,ListNode *lst2)
{
if (!lst2)
return lst1;
if (!lst1)
return lst2;
ListNode *ptr_lst1 = lst1;
ListNode *ptr_lst2 = lst2;
ListNode *begin = (ListNode *)malloc(sizeof(ListNode));
ListNode *tail = begin;
tail->next = NULL;
while (ptr_lst1 && ptr_lst2)
{
if (ptr_lst1->val < ptr_lst2->val)
{
tail->next = ptr_lst1;
ptr_lst1 = ptr_lst1->next;
tail->next->next = NULL;
tail = tail->next;
}
else
{
tail->next = ptr_lst2;
ptr_lst2 = ptr_lst2->next;
tail->next->next = NULL;
tail = tail->next;
}
}
if (!ptr_lst1 && ptr_lst2) // lst1 is empty and lst2 has some
tail->next = ptr_lst2;
if (!ptr_lst2 && ptr_lst1) // lst1 has some and lst2 is empty
tail->next = ptr_lst1;
return begin->next;
} int countLength(ListNode *head)
{
int count = ;
while (head)
{
count ++;
head = head->next;
}
return count;
} ListNode *dividedLst(ListNode *lst1,int mid)
{
ListNode *pre = lst1;
int count = ;
while (lst1)
{
if (mid == count)
{
pre->next = NULL;
return lst1;
}
count ++;
pre = lst1;
lst1 = lst1->next;
}
}
};
sort函数中,计算长度是n,合并是n,找中点是n/2,常数个n还是n
因此时间复杂度就是O(n log n)
【原创】leetCodeOj --- Sort List 解题报告的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- 【原创】leetCodeOj --- Min Stack 解题报告
题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...
- 【原创】leetCodeOj --- Dungeon Game 解题报告
原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- 【原创】leetCodeOj ---Partition List 解题报告
原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...
- 【原创】leetCodeOj --- Interleaving String 解题报告
题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3 ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
随机推荐
- Android 表格布局<TableLayout>
表格布局即,tableLayout,表格布局通过行.列的形式来管理UI组件,TablelLayout并不需要明确地声明包含多少行.多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数, ...
- ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六)
原文:ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六) ThinkPHP多应用配置技巧(没有使用分组,这是通过入口文件产生的Home.Admin)----很实用! 比如:现在有Ho ...
- window.open()具体解释及浏览器兼容性问题
一.基本的语法:window.open(pageURL,name,parameters)当中:pageURL 为子窗体路径name 为子窗体名字parameters 为窗体參数(各參数用逗号分隔) ...
- SVN最有效的方法打基线
笔者:张克强 在微博上:张克强-敏捷307 2014/7/6 方法一来自于我的一条微博: 组织级scm建一个名为controlled的文件夹,当项目某文档通过评审后,组织级scm从项目文件夹下找 ...
- Routing 服务
WCF Routing 服务 WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发, ...
- ssh, maven and eclipse
那些破事
Unix根据该理念keep it simple, keep it stupid.可在j2ee有keep it complex, keep it smart. 所以,我彻底晕菜. 最后能活着把sprin ...
- MyEclipse导入主题文件epf后xml及jsp等页面中点击标签之后显示灰白
MyEclipse导入主题文件epf后xml及jsp等页面中点击标签之后显示灰白,症状例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVVAxOT ...
- Http协议学习总结(转)
因为项目中很多地方都与Http协议有关,零散的了解了一下Http协议,但是没有系统的学习过. 今天根据网上其他同学的整理,加上我的一些经验,我也整理了一份.当做学习记录吧. 一.什么是HTTP协议 H ...
- like-minded 都有什么意思_百度知道
like-minded 都有什么意思_百度知道 like-minded 都有什么意思
- Replace - with an en dash character (–, –) ?
这个安卓开发过程中eclipse的提示,新浪网友给出这个解决方法:http://blog.sina.com.cn/s/blog_5ea8670101015dgk.html 太笨了. 看看stacko ...