当前位置:网站首页>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 .边栏推荐
- What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
- Code or script to speed up the video playback of video websites
- Not for 60 days, magical dictionary
- (.*?) regular expression
- 2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
- 不会就坚持58天吧 实现前缀树
- 有一种密码学专用语言叫做ASN.1
- Wechat applet parameter transfer
- 12.优先级队列和惰性队列
- Shielding ODBC load balancing mode in gbase 8A special scenarios?
猜你喜欢

从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)

Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation

Value transmission and address transmission of C language, pointer of pointer

不会就坚持60天吧 神奇的字典

RMAN do not mark expired backups

15.federation

11.备份交换机

通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值

2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology

你真的会写Restful API吗?
随机推荐
一个公司的面试笔记
不会就坚持60天吧 神奇的字典
Do you have a boss to help me check whether the parameter configuration of the Flink SQL connection Kafka authentication Kerberos is wrong
14.haproxy+keepalived负载均衡和高可用
How to query the submission number of a version
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
rman不标记过期备份
The pit I walked through: the first ad Sketchpad
SQL server how to judge when the parameter received by the stored procedure is of type int?
openFeign异步调用问题
View partition table format
The function "postgis_version" cannot be found when installing PostGIS
从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)
14. Haproxy+kept load balancing and high availability
9.延迟队列
编译与链接
Taobao product details interface (product details page data interface)
Pix2.4.8 from start to installation (2021.4.4)
Differences and principles of bio, NiO and AIO
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation