今日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 解题报告的更多相关文章

  1. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  3. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  4. 【原创】leetCodeOj --- Min Stack 解题报告

    题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...

  5. 【原创】leetCodeOj --- Dungeon Game 解题报告

    原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...

  6. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  7. 【原创】leetCodeOj ---Partition List 解题报告

    原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...

  8. 【原创】leetCodeOj --- Interleaving String 解题报告

    题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3  ...

  9. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

随机推荐

  1. Codeforces Round #309 (Div. 2) C

    题意: 就是给出总共同拥有k种颜色.每种颜色有ki种,排列必须满足第i+1种的最后一种颜色必须在第i种最后一种颜色的后面,其它颜色任意.总共同拥有多少种排列点的方法. 分析: 如果d[i]表示前i种的 ...

  2. SQL Server 数据的添加修改删除和查询

    数据的添加: 首先建立一个数据库,点击新建查询,然后用代码建立一个表,表里写上列名和数据类型,约束可加可不加 然后使用insert语句往表里添加数据 insert [into] 表名 (列名1,列名2 ...

  3. ThinkPHP 的模型使用对数据库增删改查(五)

    原文:ThinkPHP 的模型使用对数据库增删改查(五) ThinkPHP 的模型使用 // 直接连接数据库,但是得先去配置文件中配置下才行 class IndexAction extends Act ...

  4. 【BASH】自己主动清理rman脚本备份文件

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  5. MyCAT部署及实现读写分离(转)

    MyCAT是mysql中间件,前身是阿里大名鼎鼎的Cobar,Cobar在开源了一段时间后,不了了之.于是MyCAT扛起了这面大旗,在大数据时代,其重要性愈发彰显.这篇文章主要是MyCAT的入门部署. ...

  6. NET Core 1.0 RC2

    NET Core 1.0 RC2 历险之旅 文章背景:对于.NET Core大家应该并不陌生, 从它被 宣布 到现在已经有1-2年的时间了,其比较重要的一个版本1.0 RC2 也即将发布..Net C ...

  7. JavaScript 基础优化(读书笔记)

    1.带有 src 属性的<script>元素不应该在其<script>和</script>标签之间再包含额外的 JavaScript 代码.如果包含了嵌入的代码,则 ...

  8. IOS获取来电去电来电归属系统通知达到效果(一)

    这里的原理是获得到呼叫系统通知,然后根据本地呼叫电话号码,相应的电话联系. 一世.   来电显示是不是在地址簿中的联系人, 这是当第一个加入一个临时的联系人在您的电话簿(它是创建一个新的接触,并保存相 ...

  9. UVA 5875 DP

    题意:给你一堆二维点,每个点有一些分数. 现在要从点(0 , 0 )出发,只能从标号小的点走到大的点,每个人有一个走的距离的限制,问最后能拿到的最高的分数,当然这个人从(0 , 0)出发还得回到( 0 ...

  10. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...