当前位置:网站首页>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
边栏推荐
- 消息的订阅和发布
- Comparison between agile development and Devops
- .net6与英雄联盟邂逅之——根据官方LCU API制作游戏助手
- Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
- Pytoch learning notes (I) installation and use of common functions
- gdb常用命令
- 敏捷开发与DevOps的对比
- Pytoch learning notes (II) the use of neural networks
- 基于多任务深度学习的实体和事件联合抽取模型
- @千行百业,一起乘云而上!
猜你喜欢

Disease knowledge discovery based on spo semantic triples

基于标签嵌入注意力机制的多任务文本分类模型

IDEA(warning)No artifacts configured

Pytoch learning notes (I) installation and use of common functions

「中高级试题」:MVCC实现原理是什么?

GDB common commands

Basic syntax of MySQL DDL and DML and DQL

Large and small end mode
![[NOIP2003 普及组]栈](/img/95/871b1c6f492b467bffd25912304b44.gif)
[NOIP2003 普及组]栈

@A thousand lines of work, ride the cloud together!
随机推荐
Pytoch learning notes (III) use, modification, training (cpu/gpu) and verification of the model
[noip2003 popularity group] stack
Convert the array in JSON file to struct
Propagation of transactions
Segmentation fault (core dumped)
JS get the current time, time and timestamp conversion
2022-07-26日报:Alphafold DB数据库建立一周年,官推盘点亮点研究
Subscription and publication of messages
Sequence traversal of binary tree (implemented in C language)
【论文阅读】GRAW+:A Two-View Graph Propagation Method With Word Coupling for Readability Assessment
The difference between V-model and.Sync modifier
C language_ Combination of structure and array
Joint entity and event extraction model based on multi task deep learning
JS download files, filesaver.js export txt and Excel files
Research on technology subject division method based on patent multi-attribute fusion
Pytoch learning notes (I) installation and use of common functions
Inspiration from brain: introduction to synaptic integration principle in deep neural network optimization
.net6与英雄联盟邂逅之——根据官方LCU API制作游戏助手
Pytoch learning notes (II) the use of neural networks
Solve the problem that JUnit of idea console cannot be input with scanner