当前位置:网站首页>不会就坚持71天吧 链表排序
不会就坚持71天吧 链表排序
2022-07-29 04:13:00 【一只小小明】
自顶向下归并排序
对链表自顶向下归并排序的过程如下。
找到链表的中点,以中点为分界,将链表拆分成两个子链表。寻找链表的中点可以使用快慢指针的做法,快指针每次移动 22 步,慢指针每次移动 11 步,当快指针到达链表末尾时,慢指针指向的链表节点即为链表的中点。
对两个子链表分别排序。
将两个排序后的子链表合并,得到完整的排序后的链表。
上述过程可以通过递归实现。递归的终止条件是链表的节点个数小于或等于 11,即当链表为空或者链表只包含 11 个节点时,不需要对链表进行拆分和排序。
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;
}
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/7WHec2/solution/lian-biao-pai-xu-by-leetcode-solution-0rjx/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。边栏推荐
- openFeign异步调用问题
- 有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
- 店铺排名问题,如何解决?
- The principle of inverse Fourier transform (IFFT) in signal processing
- (.*?) regular expression
- Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
- When array is used as a function parameter, it is better to use the array size as a function parameter
- Fuzzy query of SQL
- Mmdetection preliminary use
- 对一个元素使用多种变形的方法
猜你喜欢

MPU6050

When array is used as a function parameter, it is better to use the array size as a function parameter

数据挖掘——关联分析例题代码实现(下)

伏英娜:元宇宙就是新一代互联网!

C语言实现三子棋游戏(详解)

AssertionError(“Torch not compiled with CUDA enabled“)

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

Why are there so many unknowns when opengauss starts?

HCIP BGP

Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
随机推荐
BIO、NIO、AIO的区别和原理
Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
C language - character array - string array - '\0' -sizeof-strlen() -printf()
Design of environment detection system based on STM32 and Alibaba cloud
Object detection: object_ Detection API +ssd target detection model
当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
GBase 8a特殊场景下屏蔽 ODBC 负载均衡方式?
[kvm] install KVM
通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
SQL server当存储过程接收的参数是int类型时,如何做判断?
Three tier architecture of enterprise network
数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
初识C语言(3)
[kvm] create virtual machine from kickstart file
大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?
BGP的基础配置---建立对等体、路由宣告
Code or script to speed up the video playback of video websites
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)
Pointer variables -printf%d and%p meaning
MySQL gets the maximum value record by field grouping