今日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. C#反射Assembly 具体说明

    1.对C#反射机制的理解 2.概念理解后,必须找到方法去完毕,给出管理的主要语法 3.终于给出有用的样例,反射出来dll中的方法 反射是一个程序集发现及执行的过程,通过反射能够得到*.exe或*.dl ...

  2. [WebGL入门]十,矩阵计算和外部库

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:],另外,鄙人webgl研究还不够深入,一些专业词语,假设翻译有误,欢迎大家指 ...

  3. Android中G-Sensor相关流程

    1.使G-sensor正常工作需要做的事: G-sensor driver文件包括: driver/i2c/chips/lis331dl.c driver/i2c/chips/sensorioctl. ...

  4. 【C#遗补】之Char.IsDigit和Char.IsNumber的区别

    原文:[C#遗补]之Char.IsDigit和Char.IsNumber的区别 Char中IsDigit和IsNumber的两个方法都是用来判断字符是否是数字的,那他们有什么区别 IsDigit    ...

  5. 【LeetCode】Reorder List 解题报告

    Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do th ...

  6. Phalcon之 表单(Forms)

    Phalcon中提供了 Phalcon\Forms组件以方便开发人员创建和维护应用中的表单. 以下的样例中展示了主要的用法: <?php use Phalcon\Forms\Form, Phal ...

  7. Jersey框架三:Jersey对HTTPS的支持

    Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...

  8. The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8(转)

    配置项目,从tomcat低版本,放到tomcat8时,正常的项目居然报错了: The method getDispatcherType() is undefined for the type Http ...

  9. SSL与TLS的区别以及介绍(转)

    SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议层.SSL通过互相认证.使用数字签名确保完整性.使用加密确保私密性,以实现客户 ...

  10. java反射中Method类invoke方法的使用方法

    package com.zsw.test; import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetExcep ...