当前位置:网站首页>Leetcode 148. sorting linked list
Leetcode 148. sorting linked list
2022-07-27 22:05:00 【henujolly】
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 dummy=new ListNode(0);
ListNode temp = dummy, tmp1 = head1, tmp2 = head2;
while(tmp1!=null&&tmp2!=null){
if(tmp1.val<tmp2.val){
temp.next=tmp1;
tmp1=tmp1.next;
}
else{
temp.next=tmp2;
tmp2=tmp2.next;
}
temp=temp.next;
}
if(tmp1!=null) temp.next=tmp1;
else if(tmp2!=null) temp.next=tmp2;
return dummy.next;
}
}
边栏推荐
- Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
- 【StoneDB故障诊断】系统资源瓶颈诊断
- V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
- @The difference between Autowired annotation and @resource annotation
- 软件测试面试题:请说出这些测试最好由那些人员完成,测试的是什么?
- The design idea of relational database is obvious to you in 20 pictures
- Shengyang technology officially launched the remote voiceprint health return visit service system!
- 8000 word explanation of OBSA principle and application practice
- First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
- [question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
猜你喜欢

项目分析(从技术到项目、产品)

ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现

How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath

How to use Fiddler for weak network testing

项目分析(哪些是it培训给不了)

学完4种 Redis 集群方案要多久?我一口气给你说完

How to realize a good knowledge management system?

每条你收藏的资讯背后,都离不开TA

Log4j vulnerability is still widespread and continues to cause impact

Talk about MySQL transaction two-phase commit
随机推荐
技术管理 - 一定要抓大放小
Software testing interview question: what is the focus of unit testing, integration testing, and system testing?
2021-11-05 understanding of class variables and class methods
Implementation of arbitrary code execution based on.Net dynamic compilation technology
Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction
@Component可以和@Bean 用在同一个类上吗?
How to realize a good knowledge management system?
排序(冒泡排序)后面学习持续更新其它排序方法
Monitor the running of server jar and restart script
It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years
QT take out the input box string, lineedit
数组扩容、排序、嵌套语句应用
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
软件测试面试题:通过画因果图来写测试用例的步骤为___、___、___、___及把因果图转换为状态图共五个步骤。 利用因果图生成测试用例的基本步骤是?
[question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
Interview questions that big companies need to prepare
[C language] high precision addition, subtraction, multiplication and division template
MySQL execution process and order
Finish learning redis cluster solution at one go
B站崩了,如果我们是那晚负责修复的开发人员