当前位置:网站首页>148. Sorting linked list
148. Sorting linked list
2022-07-21 22:40:00 【Anji_ lh1029】
1、 Title Description
2、 Topic analysis
According to the meaning , Sort the linked list into 【 Ascending 】 Pattern .
The picture above is very clear .
1、 Find the linked list 【 Intermediate nodes 】(LeetCode-876 topic , May refer to :876. The middle node of a list )
2、 Sort two linked lists 【 Merge two ordered lists 】(LeetCode21 topic , May refer to :21. Merge two ordered lists )
3、 Recursion above 1 and 2 Step by step .
Therefore, generally complex topics are several simple topics 【 Sum up and 】.
So the code is as follows :
class Solution {
public ListNode sortList(ListNode head) {
// 1、 Recursion end condition
if (head == null || head.next == null) {
return head;
}
// 2、 Find the middle node of the linked list and disconnect the linked list & Recursive downward exploration
// Split the list , Divide and conquer
ListNode middle = getMiddle(head);
// The second half
ListNode rightHead = middle.next;
// The first half is separated in the middle
middle.next = null;
// Split recursively and sequentially
ListNode left = sortList(head);
ListNode right = sortList(rightHead);
// 3、 Current layer business operations ( Merge ordered linked list )
return mergeListNode(left,right);
}
// Get intermediate nodes 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;
}
// Merge and sort two linked lists 【 Ascending 】 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;
}
}
Complexity analysis
Time complexity :O(nlogn), among n It's the length of the list .
Spatial complexity :O(logn), among n It's the length of the list . The space complexity mainly depends on the stack space of recursive calls
边栏推荐
- 验证哥德巴赫猜想
- What is PCBA? What is the importance of PCBA testing?
- This Bluetooth chip giant aims at the WiFi SOC market and launches a low-power WiFi MCU product line
- 016: simple calculator
- 328. 奇偶链表
- 好看又有趣的数据可视化图表制作,膜拜教程
- Do you think sub database and sub table are really suitable for your system? Talk about how to select sub databases, sub tables and newsql
- The robotframework (ride) keyword cannot be used or the keyword is black
- 西瓜书第三章-线性模型
- The evolution of data warehouse in recent 10 years and suggestions on database learning
猜你喜欢
Explain the principle, classification and function of microphone array in detail
线性回归大结局(岭(Ridge)、 Lasso回归原理、公式推导),你想要的这里都有
Blockbuster: the domestic ide was released and developed by Alibaba. It is completely open source (high performance + high customization)
543. 二叉树的直径
110. 平衡二叉树
datart 数据可视化作品开源 | 图表插件作品全开源啦,通过百度云下载链接提取
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!
2021普及组总结
226. 翻转二叉树
「跑象科技」获得天使+融资,打造新一代实时数据基础平台
随机推荐
What are the characteristics and application fields of Bluetooth module in the Internet of things?
The robotframework (ride) keyword cannot be used or the keyword is black
Expression evaluation
Linear regression finale (ridge, Lasso regression principle, formula derivation), you want everything here
企业如何做好数据管理?产品选型怎么做?
Good looking and interesting data visualization chart making, worship tutorial
016:简单计算器
2020 popularization group summary
004: print characters
干货 | 分布式系统的数据分片难题
Pytorch2onnx2tensorflow environment preparation win10
Automated test model of 03 selenium
第十周ACM训练报告
Explain the principle, classification and function of microphone array in detail
In depth analysis of multiple knapsack problem (Part 2)
205. 同构字符串
组队
What is the gateway? What is the Internet of things gateway?
What is an electronic scale weighing module? What functions does it have?
Let you know the current situation and future development trend of wireless charging technology