当前位置:网站首页>Leetcode148 sort linked list (merge method applied to merge)
Leetcode148 sort linked list (merge method applied to merge)
2022-07-26 14:05:00 【Lin Shu ໌້ᮨ】
Here's the head of the list head , Please sort them in ascending order and return the sorted list
/**
* Using merge sort merge Method : First, disassemble the big list into a small list , Then combine the small linked list back to an ascending large linked list
* Find the middle point of the linked list when splitting : Speed pointer
* Merge two ascending linked lists : see leetcode21
*/
public class Num148_SortList {
private static class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public static ListNode sortList(ListNode head) {
return sortListHelper(head,null);
}
// Split the list
private static ListNode sortListHelper(ListNode head, ListNode tail) {
if(head==null){
return head;
}
if(head.next==tail){
head.next=null;
return head;
}
// Find the middle point of the linked list : Speed pointer
ListNode slow=head;
ListNode fast=head;
while (fast!=tail){
fast=fast.next;
slow=slow.next;
if(fast!=tail){
fast=fast.next;
}
}
// At this time, the intermediate node of the linked list is found
ListNode mid=slow;
// Split the left linked list
ListNode leftList=sortListHelper(head,mid);
// Split the right linked list
ListNode rightList=sortListHelper(mid,tail);
// At this time, the linked list is split , Conduct merge Operation merge linked list
return merge(leftList,rightList);
}
private static ListNode merge(ListNode leftlist, ListNode rightlist) {
// Merge two linked lists ( Ascending ); Return the merged linked list
if(leftlist==null){
return leftlist;
}
if(rightlist==null){
return rightlist;
}
// At this time, both linked lists are not empty , Merge two linked lists
// Create a virtual head node , Insert with tail
ListNode dummyHead=new ListNode(10*10*10*10*10+1);
ListNode cur=dummyHead;
while (leftlist!=null&&rightlist!=null){
if(leftlist.val<=rightlist.val){
cur.next=leftlist;
cur=leftlist;
leftlist=leftlist.next;
}else{
cur.next=rightlist;
cur=rightlist;
rightlist=rightlist.next;
}
// Judge left And right Whether to traverse to null
if(leftlist==null){
cur.next=rightlist;
}
if(rightlist==null){
cur.next=leftlist;
}
}
return dummyHead.next;
}
public static void main(String[] args) {
ListNode node1=new ListNode(-1);
ListNode node2=new ListNode(5);
ListNode node3=new ListNode(3);
ListNode node4=new ListNode(4);
ListNode node5=new ListNode(0);
node1.next=node2;
node2.next=node3;
node3.next=node4;
node4.next=node5;
ListNode res=sortList(node1);
System.out.println(res);
}
}
Related issues :
Power button 147- Insert and sort the linked list
边栏推荐
- 图书下载 | 2022年《终身监督学习导论》Meta AI、CMU等学者合著,171页PDF
- [oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer
- Technology sharing | gtid that needs to be configured carefully_ mode
- Redis的数据操作
- 大小端模式
- @千行百业,一起乘云而上!
- Digital collections accelerate the breaking of the circle and help the industry find new opportunities
- Share 44 JS problems, and half of them are masters
- C language Snake linked list and pointer practice
- Basic knowledge about memory chips
猜你喜欢

【黑马早报】字节旗下多款APP下架;三只松鼠脱氧剂泄露致孕妇误食;CBA公司向B站索赔4.06亿;马斯克否认与谷歌创始人妻子有婚外情...

Technology evolution analysis framework based on two-level topic model and its application

基于专利多属性融合的技术主题划分方法研究

循环队列(c语言实现)

Focus on building four "highlands" and join hands with partners to build the national cloud!

Polymorphic case - making drinks

MySql的DDL和DML和DQL的基本语法

"Intermediate and advanced test questions": what is the implementation principle of mvcc?

万字长文,浅谈企业数字化建模蓝图
![[dark horse morning post] many apps under bytek have been taken off the shelves; The leakage of deoxidizer in three squirrels caused pregnant women to eat by mistake; CBA claimed 406million yuan from](/img/f6/03e39799db36c33a58127359aa2794.png)
[dark horse morning post] many apps under bytek have been taken off the shelves; The leakage of deoxidizer in three squirrels caused pregnant women to eat by mistake; CBA claimed 406million yuan from
随机推荐
Pytorch学习笔记(三)模型的使用、修改、训练(CPU/GPU)及验证
The difference between V-model and.Sync modifier
C language_ Structure pointer to access structure array
OA项目之会议排座和送审
技术分享 | 需要小心配置的 gtid_mode
ISCC2021 LOCKK题解
[oauth2] VII. Wechat oauth2 authorized login
Jenkins 中 shell 脚本执行失败却不自行退出
大脑带来的启发:深度神经网络优化中突触整合原理介绍
Pytoch learning notes (II) the use of neural networks
大小端模式
Basic syntax of MySQL DDL and DML and DQL
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
12437 words, take you to explore the principle of RPC communication
力扣------字符串中的单词数
Ten thousand words long article, talking about the blueprint of enterprise digital modeling
基于用户画像的在线健康社区用户流失预测研究
The picture moves horizontally with the phone - gyroscope. 360 degree setting conditions
消息的订阅和发布
Research on Chinese medicine assisted diagnosis and treatment scheme integrating multiple natural language processing tasks -- taking diabetes as an example