今日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. [转]PHP 5.2~5.6 对照以及功能具体解释

    [分享]PHP 5.2~5.6 对照以及功能具体解释 作者:流水理鱼wwek 来源:http://www.iamle.com/archives/1530.html 截至眼下(2014.2), PHP ...

  2. CDOJ 1221 Ancient Go

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1221 题目分类:dfs 代码: #include<bits/stdc++.h> using na ...

  3. JavaScript引用类型之Object类

    ECMAScript中的Object类跟Java中的Object类相似,ECMAScript中的全部类都由这个类继承而来,Object类中的全部属性和方法都会出如今其他类中,所以理解Object类,就 ...

  4. linux c socket 案源

    service结束 #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #inclu ...

  5. oracle乱码问题

    oracle乱码问题通常是因为oracle字符集设置和操作系统字符集设置不一致造成的,这里不得不提到两个操作系统环境变量,LANG和NLS_LANG LANG是针对Linux系统的语言.地区.字符集的 ...

  6. Oracle Cursor的使用

    When Oracle Database executes a SQL statement, it stores the result set and processing information i ...

  7. windows 7多点触摸开发

    win7 触摸屏系统应用广泛,软件操作方便,功能强大,现以被很多硬件厂商应用. 我曾用一台装有win7 的汉王平板电脑进行了多点触摸软件的开发.      开发环境及条件: 1. 平板电脑+ win7 ...

  8. poj1087(最大流)

    传送门:A Plug for UNIX 题意:有插座用电器和适配器,用电器有插头,适配器本身有一个插孔和插头,它的作用是可以把别的插头插入到适合该适配器插孔的适配器,然后就可以用适配器的插头接到适合的 ...

  9. mysql相关日志汇总

    日志作为重要的查询问题的手段.所以尽量记录上自己须要的日志.以供自己查询一些问题. MySQL有下面几种日志: 错误日志: -log-err 查询日志: -log 慢查询日志: -log-slow-q ...

  10. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...