1. Sort List

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

解法1 归并排序。用两个函数实现:

  • merge:将两个有序链表合在一起
  • merge_sort:将无序链表排序

找中间位置用双指针

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head) {
if(head == NULL || head->next == NULL)return head;
ListNode *mid, *pre;
find_mid(head, mid, pre);
pre->next = NULL;
return merge(sortList(head), sortList(mid));
}
// 找中点时,需要对mid和pre做修改,因此需要传引用
void find_mid(ListNode* s, ListNode* &mid, ListNode* &pre){
ListNode *pp = s;
mid = s;
while(pp && pp->next){
pre = mid;
mid = mid->next;
pp = pp->next->next;
}
}
ListNode* merge(ListNode* s1, ListNode* s2){
ListNode *head = new ListNode, *p = s1, *q = s2;
ListNode *cur = head;
while(p != NULL && q != NULL){
if(p->val < q->val){
cur->next = p;
p = p->next;
}else{
cur->next = q;
q = q->next;
}
cur = cur->next;
}
if(p != NULL)cur->next = p;
else cur->next = q;
return head->next;
}
};

解法2 快速排序。partition过程中,可以用两个链表small和large分别存储pivot左侧和右侧的数据

class Solution {
public:
ListNode* sortList(ListNode* head) {
if(!head || !head->next) return head; ListNode* cur = head->next;
ListNode* small = new ListNode(0);
ListNode* large = new ListNode(0);
ListNode* sp = small;
ListNode* lp = large;
// partition
while(cur){
if(cur->val<head->val){
sp->next = cur;
sp = cur;
}
else{
lp->next = cur;
lp = cur;
}
cur = cur->next;
}
sp->next = NULL;
lp->next = NULL;
small=sortList(small->next);
large=sortList(large->next);
cur = small;
if(cur){
while(cur->next) cur = cur->next;
cur->next = head;
head->next = large;
return small;
}else{
head->next = large;
return head;
}
}
};

【刷题-LeetCode】148 Sort List的更多相关文章

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

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

  2. LeetCode刷题------------------------------LeetCode使用介绍

    临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...

  3. 【leetcode刷题笔记】Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找 ...

  4. 【刷题-LeetCode】147 Insertion Sort List

    Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...

  5. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

  6. [LeetCode] 148. Sort List 解题思路

    Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...

  7. Java for LeetCode 148 Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...

  8. leetcode 148. Sort List ----- java

    Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...

  9. Leetcode#148 Sort List

    原题地址 链表归并排序 真是恶心的一道题啊,哇了好多次才过. 代码: void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNo ...

随机推荐

  1. List集合取其中指定几条数据

    newList= oldList.subList(start, end); start,end分别是第几个到第几个,截取的内容包含前不包含结尾,用下标索引 此方法会改变原始list列表,返回的这个子列 ...

  2. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

  3. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  5. <学习opencv>图像、视频和数据文件

    /*=========================================================================*/ // openCV中的函数 /*====== ...

  6. BL8810|USB2.0高速闪存读卡器|BL8810芯片

    BL8810是由台湾旺玖半导体推出的一款USB2.0 SD/MMC闪存读卡器单芯片.支持USB2.0高速传输,并符合通用串行总线规范.该芯片集成了一个高速的8051微处理器和一个最好的数据引擎,它的引 ...

  7. BL8810|USB2.0高速闪存读卡器芯片|BL8810规格书

    1.说明 BL8810是一款USB 2.0读卡器控制器,采用高度集成的单芯片解决方案,旨在提供USB2.0和SD.SDHC.mini SD.Micro SD(T-Flash)接口规范之间的高速数据传输 ...

  8. Hadoop用户配置免密登陆

    Hadoop用户配置免密登陆, 参考其他免密配置方法自己总结的更简洁的步骤. 要实现A免密登陆B,需要把A生成的公钥放到B的对应目录下, 要实现ABC之间免密登陆,把3者的公钥汇总到一个文件中, 然后 ...

  9. Java 获取客户端浏览器中的语言设置

    获取客户端的首选语言 javax.servlet.ServletRequest.getLocale() 根据Accept-Language请求头返回客户端的首选语言.如果客户端请求没有Accept-L ...

  10. python自动化测试框架的unittest与pytest前后置条件的区别

    前言: 笔者先试有用过unittest的前后置条件和pytest的前后置条件,觉得pytest的前后置条件比unittest的要简洁.方便很多.不过在使用unittest的前后置条件时,已经觉得在和每 ...