当前位置:网站首页>148. 排序链表
148. 排序链表
2022-07-21 05:15:00 【安吉_lh1029】
1、题目描述
2、题目分析
根据题意,把链表排序成【升序】模式。
上面这个图很清晰。
1、找到链表的【中间节点】(LeetCode-876题,可参考:876. 链表的中间结点)
2、两个链表进行排序【合并两个有序链表】(LeetCode21题,可参考:21. 合并两个有序链表)
3、递归上面的1和2步骤即可。
所以一般复杂的题目是几道简单题目的【汇总和】。
所以代码如下:
class Solution {
public ListNode sortList(ListNode head) {
// 1、递归结束条件
if (head == null || head.next == null) {
return head;
}
// 2、找到链表中间节点并断开链表 & 递归下探
//拆分链表,分治处理
ListNode middle = getMiddle(head);
//后半段
ListNode rightHead = middle.next;
//前半段中间分开
middle.next = null;
//分别递归依次拆分
ListNode left = sortList(head);
ListNode right = sortList(rightHead);
// 3、当前层业务操作(合并有序链表)
return mergeListNode(left,right);
}
//获取中间节点 LeetCode-876
private ListNode getMiddle(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;
}
return slow;
}
//两个链表进行合并排序【升序】 LeetCode-21
private ListNode mergeListNode(ListNode left, ListNode right){
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
while(left != null && right != null){
if(left.val < right.val){
curr.next =left;
left = left.next;
}else{
curr.next = right;
right = right.next;
}
curr = curr.next;
}
curr.next = (left!=null)?left : right;
return dummy.next;
}
}
复杂度分析
时间复杂度:O(nlogn),其中 n 是链表的长度。
空间复杂度:O(logn),其中 n 是链表的长度。空间复杂度主要取决于递归调用的栈空间
边栏推荐
- Scientific evidence: does NMN have an effect on cholecystitis, and how about the clinical effect of NMN
- Why not use scheduled tasks to close orders?
- YOLOv4(darknet版)后处理:显示置信度、保存检测框的内容到本地
- 01-自动化测试基础--(selenium八股文部分+环境配置+八大定位)
- 深入剖析斐波拉契数列
- 深入剖析多重背包问题(下篇)
- 手把手教你在服务器上用YOLOv4训练和测试数据集(保姆级)
- Visual Studio 特性进阶
- Paoding solves the fiboracci sequence and knapsack problem - analyze the optimization process of the two problems in detail, and take you to understand dynamic programming from the most basic problem!
- 使用MySQL,请善用 JSON 这张牌
猜你喜欢
Basic process of APP testing and key points of APP testing
994. 腐烂的橘子
你觉得分库分表真的适合你的系统吗?聊聊分库分表和NewSQL如何选择
西瓜书第二章-比较检验
Liteos opening
Appium+pytest+allure realizes app automated testing, which is a small test
Is there any bad effect of NMN? Deeply analyze the efficacy and effect of NMN
深入剖析多重背包问题(下篇)
堆-原理到应用——堆排序、优先级队列
Visual studio tips, functions and features
随机推荐
When using mysql, please make good use of JSON
Scientific evidence: does NMN have an effect on cholecystitis, and how about the clinical effect of NMN
Blockbuster: the domestic ide was released and developed by Alibaba. It is completely open source (high performance + high customization)
Quartz.NET c# 教程 - 课程四:Triggers
面试题:聚簇索引和非聚簇索引有什么区别?
「跑象科技」获得天使+融资,打造新一代实时数据基础平台
One article explains the problem of data fragmentation in distributed systems
What is the gateway? What is the Internet of things gateway?
Learn GCC GDB makefile together
22张图带你深入剖析前缀、中缀、后缀表达式以及表达式求值
Using two templates (recursive method and iterative method) to complete four traversal ways of binary tree
What is an electronic scale weighing module? What functions does it have?
Qt5 GUI
深入剖析多重背包问题(上篇)
Build typescript project process locally
04-unittest單元測試框架
Super detailed dry goods: appium+pytest realizes app concurrent testing
Visual Studio 使用技巧, 功能与特性
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
Robotframework -- implementation of browser drive and operation (1)