当前位置:网站首页>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 .边栏推荐
- 不会就坚持58天吧 实现前缀树
- Machine vision series 3:vs2019 opencv environment configuration
- Change the value of the argument by address through malloc and pointer
- C语言:浅谈各种复杂的声明
- C语言力扣第61题之旋转链表。双端队列与构造循环链表
- 不会就坚持70天吧 数组中第k大的数
- flink-sql 如何设置 sql执行超时时间
- 通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值
- Function pointer and callback function
- MPU6050
猜你喜欢

全屋WiFi方案:Mesh路由器组网和AC+AP

Some problems about pointers

Applet: Area scrolling, pull-down refresh, pull-up load more

Svg -- loading animation

小程序:区域滚动、下拉刷新、上拉加载更多

Jenkins 参数化构建中 各参数介绍与示例

MPU6050

Two forms of softmax cross entropy + numpy implementation

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

Record of problems encountered in ROS learning
随机推荐
数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
Change the value of the argument by address through malloc and pointer
C语言:结构体简单语法总结
pat A1041 Be Unique
The pit I walked through: the first ad Sketchpad
opengauss预检查安装
不会就坚持70天吧 数组中第k大的数
C language - character array - string array - '\0' -sizeof-strlen() -printf()
Opengauss pre check installation
Multi rotor six axis hardware selection
When array is used as a function parameter, it is better to use the array size as a function parameter
Beginner: array & String
Multi card training in pytorch
有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
Whole house WiFi solution: mesh router networking and ac+ap
Locker 2022.1.1
不会就坚持64天吧 查找插入位置
不会就坚持65天吧 只出现一次的数字
14.haproxy+keepalived负载均衡和高可用