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. LuoguB2101 计算矩阵边缘元素之和 题解

    Content 给定一个 \(m\times n\) 的矩阵,求矩阵边缘元素之和. 数据范围:\(1\leqslant m,n\leqslant 100\). Solution 对于新手来说,看到这题 ...

  2. Vue3.0是如何变快的

    1.diff算法优化 + Vue2中的虚拟dom是进行全量的对比 https://vue-next-template-explorer.netlify.app/ + Vue3新增了静态标记(Patch ...

  3. 『学了就忘』Linux系统定时任务 — 89、任务调度工具anacron

    目录 1.任务调度工具anacron介绍 2.新旧版本Linux中anacron工具的区别 3./etc/cron.{daily,weekly,monthly}目录说明 4.anacron命令 5./ ...

  4. Various methods for capturing the screen

    Explains techniques for capturing the screen programmatically. Download GDI source code - 72.1 Kb Do ...

  5. Oracle根据约束条件名称查找对应的数据

    select * from dba_constraints where constraint_name = 'SYS_C0082752'

  6. C/C++ 结构体字节对齐

    在用sizeof运算符求算某结构体所占空间时,并不是简单地将结构体中所有元素各自占的空间相加,这里涉及到内存字节对齐的问题.从理论上讲,对于任何 变量的访问都可以从任何地址开始访问,但是事实上不是如此 ...

  7. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

  8. spoj-SUBSUMS - Subset Sums

    SUBSUMS - Subset Sums Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20, ...

  9. 【剑指Offer】52. 两个链表的第一个公共节点 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:栈 方法二:HashSet 方法三:不使用额外空间 日期 ...

  10. golang(gin框架),基于RESTFUL的跨语言远程通信尝试

    golang(gin框架),基于RESTFUL的跨语言远程通信尝试 背景: 在今年的项目实训过程中,遇到了这样的问题: 企业老师讲课实用的技术栈是Java springboot. 实训实际给我们讲课以 ...