当前位置:网站首页>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
边栏推荐
- Pytest -- plug-in writing
- Redis tutorial
- IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
- Introduction and installation of Solr
- 雪崩问题以及sentinel的使用
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- Subnet division and subnet summary
- pytorch nn.AdaptiveAvgPool2d(1)
- 数据库DDL(Data Definition Language,数据定义语言)知识点
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
猜你喜欢

Stop saying that you can't solve the "cross domain" problem

LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表

jeecgboot输出日志,@Slf4j的使用方法

Detailed list of errors related to twincat3 ads of Beifu

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板

pytorch nn.AdaptiveAvgPool2d(1)

Ridge regression and lasso regression

监听器 Listener

4、【WebGIS实战】软件操作篇——数据导入及处理
随机推荐
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
multiple linear regression
pytorch nn.AdaptiveAvgPool2d(1)
详解Spark运行模式(local+standalone+yarn)
shell脚本使用两个横杠接收外部参数
[us match preparation] complete introduction to word editing formula
C语言多线程编程入门学习笔记
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
二叉树神级遍历:Morris遍历
Nacos
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Depth first traversal of C implementation Diagram -- non recursive code
Edlines: a real time line segment detector with a false detection control
Finally in promise
Avalanche problem and the use of sentinel
Bilinear upsampling and f.upsample in pytorch_ bilinear
终极套娃 2.0 | 云原生交付的封装
torch. histc
pytorch nn. AdaptiveAvgPool2d(1)
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?