当前位置:网站首页>LeetCode-归并排序链表
LeetCode-归并排序链表
2022-07-02 04:23:00 【Master_hl】
题目描述
示例分析

题目要求
一看见时间复杂度为O(N*logN),而处于这个时间复杂度的排序只有三个:堆排、快排、归并排序,快排最坏情况下,是O(N^2),而堆排不适合链表的排序,所以只剩下归并排序了。
方法一:自顶而下(递归)
自顶而下的方式,就是将链表在递出去的过程中不断拆分成两半,直到只剩下最后两个结点的时候才开始归回来合并,在归回来的时候,左右两个序列段都已经分别有序了,所以合并过程就是将两个有序序列段合并。但是这样做还是达不到完成这个题的要求,这里只是提供一种递归的思路,后面会给出迭代的思路。
代码实现
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode slow = head;
ListNode fast = head.next;
//快慢指针来分割链表
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode midR = slow.next;
slow.next = null;//将中间结点置空是下次递归 return 的必要条件
ListNode left = sortList(head);
ListNode right = sortList(midR);
//合并
ListNode newHead = new ListNode(0);
ListNode prev = newHead;;
while(left != null && right != null) {
if(left.val <= right.val) {
prev.next = left;
left = left.next;
} else {
prev.next = right;
right = right.next;
}
prev = prev.next;
}
//走到这里,两个有序序列,一个为空,一个不为空,把不为空的接在prev的后面
prev.next = (left != null ? left : right);
return newHead.next;
}画图分析

上图分析了左半部分递出去的过程,2和5合并完之后成为一个有序序列段,而 midR 这一边是一个单独的只有一个结点的有序序列段,所以就印证了前面的解释。
复杂度分析
- 时间复杂度:O(N*logN) ,就是归并排序的时间复杂度。
- 空间复杂度:O(logN),每一层的合并都用到了辅助结点,所以空间复杂度就是树的高度。
方法二:自底而上(迭代)
自顶而下为什么做不到常数个空间的空间复杂度呢,因为每递归一次,就要开辟一个辅助的栈空间,并且先执行一个函数的左,右边还没执行,所以栈空间就没有被回收,就必然导致空间复杂度较迭代来说要高。迭代的方式不像递归那样先操作整条链表,再不断细分,而是先两两有序,再四个四个有序...,最后当subLen = 链表长度的一半时,执行完本次循环,就全部有序。
代码实现
public ListNode sortList(ListNode head) {
if (head == null) {
return head;
}
int len = 0;
ListNode node = head;
while (node != null) {
len++;
node = node.next;
}
ListNode newHead = new ListNode(0, head);
//每次循环不断让左右序列扩大两倍
for (int subLen = 1; subLen < len; subLen <<= 1) {
ListNode prev = newHead;
ListNode cur = newHead.next;
while (cur != null) {
ListNode head1 = cur;
//移动到左子序列的尾巴
for (int i = 1; i < subLen && cur.next != null; i++) {
cur = cur.next;
}
ListNode head2 = cur.next;
//分割
cur.next = null;
cur = head2;
//移动到右子序列的尾巴
for (int i = 1; i < subLen && cur != null && cur.next != null; i++) {
cur = cur.next;
}
//用next记录下一次待合并的两序列中左序列的头
ListNode next = null;
if (cur != null && cur.next != null) {
next = cur.next;
cur.next = null;
}
ListNode merged = merge(head1, head2);
//将合并的序列串在辅助结点之后
prev.next = merged;
while (prev.next != null) {
prev = prev.next;
}
cur = next;
}画图分析

从图中可以明显看出迭代与递归的区别,也印证了前面说的先两两有序,再四个四个有序,当subLen = 1/2 len 的时候,执行完本次循环就全部有序。且迭代真正做到了开辟常数个栈空间。
复杂度分析
- 时间复杂度: O(N*logN)
- 空间复杂度: O(1)
本篇完!!!
边栏推荐
- BGP experiment the next day
- Introduction to JSON usage scenarios and precautions
- [untitled]
- How much is the tuition fee of SCM training class? How long is the study time?
- Hands on deep learning (II) -- multi layer perceptron
- Go branch and loop
- Sorted out an ECS summer money saving secret, this time @ old users come and take it away
- Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
- Read "the way to clean code" - function names should express their behavior
- A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
猜你喜欢

Federal learning: dividing non IID samples according to Dirichlet distribution

Playing with concurrency: what are the ways of communication between threads?

Play with concurrency: what's the use of interruptedexception?

Www2022 | know your way back: self training method of graph neural network under distribution and migration

A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent

藍湖的安裝及使用

Installation et utilisation du lac bleu

蓝湖的安装及使用

How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of

Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!
随机推荐
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
Fluent icon demo
BGP experiment the next day
powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
One click generation and conversion of markdown directory to word format
Typescript practice for SAP ui5
Binary tree problem solving (2)
Li Kou interview question 02.08 Loop detection
60后关机程序
Pytorch---使用Pytorch进行鸟类的预测
Message mechanism -- message processing
Go language naming specification
The core idea of performance optimization, dry goods sharing
Hand tear - sort
Which insurance company has a better product of anti-cancer insurance?
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
Ognl和EL表达式以及内存马的安全研究
Sword finger offer II 006 Sort the sum of two numbers in the array
Document declaration and character encoding