今日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. ezw证件照芯片压缩算法

    相关网站:http://m.blog.csdn.net/blog/kimwu/12654517 http://blog.sina.com.cn/s/blog_4be751690100bsgb.html ...

  2. Maven聚合

    <project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2 ...

  3. Java并发编程--Fork/Join框架使用

    上篇博客我们介绍了通过CyclicBarrier使线程同步,可是上述方法存在一个问题,那就是假设一个大任务跑了2个线程去完毕.假设线程2耗时比线程1多2倍.线程1完毕后必须等待线程2完毕.等待的过程线 ...

  4. ACM起步要点总结(转哈工大)

    首先,我想说的就是,我是一个很普通的ACMer,高中没有参加过任何计算机和数学竞赛的经历,也没有ben那样过人的天资,努力至今也未能取得什么成绩,我之所以写下这篇文章,只是希望给刚进大学或者刚进ACM ...

  5. Linux下Apache PHP Mysql默认安装路径

    Apache 假设採用RPM包安装.安装路径应在 /etc/httpd文件夹下 Apache配置文件: /etc/httpd/conf/httpd.conf Apache模块路径: /usr/sbin ...

  6. ThinkPHP的连贯操作方法中field方法

    ThinkPHP的连贯操作方法中field方法有很多的使用技巧,field方法主要目的是标识要返回或者操作的字段,下面详细道来. 1.用于查询 在查询操作中field方法是使用最频繁的. $Model ...

  7. Android 布局之LinearLayout 子控件weight权重的作用详析(转)

    关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...

  8. Hama学习总结

    Hama学习笔记 1.       Hama定义 Hama是基于HDFS上的BSP模型实现,其执行不须要MapReduce. 例证例如以下: 在单点调试的Hama系统上,仅仅执行NameNode.Da ...

  9. 怎样使用 iOS 7 的 AVSpeechSynthesizer 制作有声书(1)

    原文: http://www.raywenderlich.com/64623/make-narrated-book-using-avspeechsynthesizer-ios-7 随着 PageVie ...

  10. Python数据结构-序表

    序表解包: list=['aa','bb','cc'] [a1,a2,a3]=list