当前位置:网站首页>Won't you insist on 71 days? List sorting
Won't you insist on 71 days? List sorting
2022-07-29 04:16:00 【A little Ming】
Top down merge sort
The process of merging and sorting the linked list from top to bottom is as follows .
Find the midpoint of the list , With the midpoint as the boundary , Split the linked list into two sub linked lists . To find the midpoint of the linked list, you can use the fast and slow pointer , Every time the fast pointer moves 22 Step , Each time the slow pointer moves 11 Step , When the fast pointer reaches the end of the linked list , The linked list node pointed to by the slow pointer is the midpoint of the linked list .
Sort the two sub linked lists separately .
Merge the two sorted sub linked lists , Get the complete sorted linked list .
The above process can be implemented recursively . The termination condition of recursion is that the number of nodes in the linked list is less than or equal to 11, That is, when the linked list is empty or the linked list only contains 11 When a node , There is no need to split and sort the linked list .
class Solution {
public ListNode sortList(ListNode head) {
return sortList(head, null);
}
public ListNode sortList(ListNode head, ListNode tail) {
if (head == null) {
return head;
}
if (head.next == tail) {
head.next = null;
return head;
}
ListNode slow = head, fast = head;
while (fast != tail) {
slow = slow.next;
fast = fast.next;
if (fast != tail) {
fast = fast.next;
}
}
ListNode mid = slow;
ListNode list1 = sortList(head, mid);
ListNode list2 = sortList(mid, tail);
ListNode sorted = merge(list1, list2);
return sorted;
}
public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummyHead = new ListNode(0);
ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
while (temp1 != null && temp2 != null) {
if (temp1.val <= temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else if (temp2 != null) {
temp.next = temp2;
}
return dummyHead.next;
}
}
author :LeetCode-Solution
link :https://leetcode.cn/problems/7WHec2/solution/lian-biao-pai-xu-by-leetcode-solution-0rjx/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .边栏推荐
- 12. Priority queue and inert queue
- Semantic segmentation correlation
- 数据库SQL语句实现数据分解的函数查询
- Codeforces round 810 (Div. 2) d. rain (segment tree difference)
- 15.federation
- Object detection: object_ Detection API +ssd target detection model
- [hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
- 请问,在sql client中,执行insert into select from job时,如何单
- Pointer variables -printf%d and%p meaning
- 不会就坚持69天吧 合并区间
猜你喜欢

Machine vision Series 2: vs DLL debugging

Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)

不会就坚持62天吧 单词之和

Two forms of softmax cross entropy + numpy implementation

Object detection: object_ Detection API +ssd target detection model

Can you really write restful API?

14.haproxy+keepalived负载均衡和高可用

11. Backup switch

No, just stick to it for 59 days

Fu Yingna: Yuan universe is the new generation of Internet!
随机推荐
How to write the filter conditions of data integration and what syntax to use? SQL syntax processing bizdate can not be
There is a special cryptology language called asn.1
Class starts! See how smardaten decomposes complex business scenarios
Mmdetection preliminary use
Record of problems encountered in ROS learning
对一个元素使用多种变形的方法
The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
Opengauss pre check installation
RMAN do not mark expired backups
The function "postgis_version" cannot be found when installing PostGIS
C语言:枚举知识点总结
Not for 58 days. Implement prefix tree
Jenkins 参数化构建中 各参数介绍与示例
SQL time fuzzy query datediff() function
Design of environment detection system based on STM32 and Alibaba cloud
Why are there so many unknowns when opengauss starts?
不会就坚持64天吧 查找插入位置
伏英娜:元宇宙就是新一代互联网!
GBase 8a特殊场景下屏蔽 ODBC 负载均衡方式?
大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?