当前位置:网站首页>Sort linked list (merge sort)
Sort linked list (merge sort)
2022-07-01 03:36:00 【Enthusiastic citizen Mr. Xue】
This question requires that O(n log n) Under time complexity and constant space complexity . In the sorting algorithm , Only quick platoon 、 The time complexity of heap and merge sort can meet the requirements , But the time complexity of fast scheduling is unstable and the space complexity is not constant , Heap is not suitable for linked list , So think about merging and sorting
public ListNode sortList(ListNode head) {
return mergeSort(head,null);
}
// Recursively separate stages
public ListNode mergeSort(ListNode head,ListNode tail){
if(head == null) return head;
When there are only two nodes left , Directly disconnect , Otherwise, it will be recursive forever
if(head.next == tail){
head.next = null;
return head;
}
Speed pointer , When the fast pointer reaches the tail node , The slow pointer will go to the middle node .
ListNode fast = head;
ListNode slow = head;
while(fast != tail){
slow = slow.next;
fast = fast.next;
if(fast != tail) fast = fast.next;
}
Record intermediate nodes , Then a recursive
ListNode mid = slow;
Recursion from head node to intermediate node
ListNode sort1 = mergeSort(head,mid);
Recursion from intermediate node to tail node
Note that you cannot start from the next in the middle like merge sorting
ListNode sort2 = mergeSort(mid,tail);
Merge section stage
ListNode res = merge(sort1,sort2);
return res;
}
// Consolidation phase
public ListNode merge(ListNode headA,ListNode headB){
Create a chain header node
ListNode dummy = new ListNode();
Temporary node , Record the end position of the linked list
ListNode temp = dummy;
ListNode tempA = headA;
ListNode tempB = headB;
while(tempA != null && tempB != null){
Put the smaller node value in front
if(tempA.val < tempB.val){
temp.next = tempA;
tempA = tempA.next;
}else{
temp.next = tempB;
tempB = tempB.next;
}
Linked list pointer backward
temp = temp.next;
}
When A Not empty , explain B It's over , Put... Directly A The rest is added to the end of the linked list
if(tempA != null) temp.next = tempA;
When B The same is true when it is not empty
if(tempB != null) temp.next = tempB;
Return to the new linked list
return dummy.next;
}
Spatial complexity log n
边栏推荐
- Let's just say I can use thousands of expression packs
- Bilinear upsampling and f.upsample in pytorch_ bilinear
- 复习专栏之---消息队列
- Basic concepts of database
- EDLines: A real-time line segment detector with a false detection control翻译
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- Feign remote call and getaway gateway
- Avalanche problem and the use of sentinel
- Detailed explanation of ES6 deconstruction grammar
- ctfshow爆破wp
猜你喜欢

Listener listener

FCN全卷积网络理解及代码实现(来自pytorch官方实现)
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

pytorch训练深度学习网络设置cuda指定的GPU可见

二叉树神级遍历:Morris遍历

Detailed list of errors related to twincat3 ads of Beifu

C语言多线程编程入门学习笔记

终极套娃 2.0 | 云原生交付的封装

Overview of EtherCAT principle

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
随机推荐
go实现命令行的工具cli
TEC: Knowledge Graph Embedding with Triple Context
Data exchange JSON
Subnet division and subnet summary
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
Ultimate dolls 2.0 | encapsulation of cloud native delivery
监听器 Listener
Hello World generation
JUC learning
Introduction and installation of Solr
Golang multi graph generation gif
Detailed explanation of ES6 deconstruction grammar
ECMAScript 6.0
复习专栏之---消息队列
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
Cookie&Session
multiple linear regression
md5sum操作
Leetcode:829. 连续整数求和
Nacos