当前位置:网站首页>LeetCode-对链表进行插入排序
LeetCode-对链表进行插入排序
2022-07-02 04:23:00 【Master_hl】
题目内容
示例分析
代码实现
public ListNode insertionSortList(ListNode head) {
if(head == null) return null;
ListNode newHead = new ListNode(0);
newHead.next = head;
ListNode SortedLast = head;
ListNode cur = SortedLast.next;
while(cur != null) {
if(SortedLast.val <= cur.val) {
SortedLast = SortedLast.next;
//继续判断下一个待排序的节点
cur = SortedLast.next;
} else {
//进入else说明要往前面插了
ListNode prev = newHead;
while(prev.next.val <= cur.val) {
prev = prev.next;
}
//有序序列中最后一个数据的后继绑上下一个待排序的数据
SortedLast.next = cur.next;
//插入元素的后继绑上有序序列中排在它之后的数据
cur.next = prev.next;
//插入元素的前一个数据的后继绑上这个插入元素
prev.next = cur;
//继续判断下一个待排序的节点
cur = SortedLast.next;
}
}
return newHead.next;
}
分析与插排的联系
关键步骤分析
我们在调整结点顺序的时候,先将 SortedLast 的后继绑住下一个待排序元素,链表是有两个域的,我习惯先绑后面,再处理前一个元素的后继。
分析上图的1,2,3的代码:
1.有序序列最后一个元素的后继总是与待排序序列第一个元素绑定起来,循环进else,说明待插入元素的值小于SortedLast,所以就把SortedLast的后继与下一个待插入的元素绑定。
2.不管有没有进while循环,prev的位置的值一定是刚好小于cur的值,while循环的作用就是调整prev指向刚好小于cur结点的值的前一个结点。
3.把prev的后继绑住插进来的cur,形成有序序列。
复杂度分析
1.时间复杂度
链表的排序不像数组,这里不需要挪数据,只需要修改指向就行,但是因为链表没有所谓的下标,所以找到插入位置的下标所需的时间复杂度就是 O(n) ,n 个数据的排序,所以时间复杂度为 O(n^2).
2.空间复杂度 O(1)
本篇完!!!
边栏推荐
- Target free or target specific: a simple and effective zero sample position detection comparative learning method
- Introduction to JSON usage scenarios and precautions
- go 语言命名规范
- Which insurance company has a better product of anti-cancer insurance?
- Pytoch --- use pytoch to realize u-net semantic segmentation
- Typescript practice for SAP ui5
- Pytorch-Yolov5从0运行Bug解决:
- office_ Delete the last page of word (the seemingly blank page)
- Go function
- Unit testing classic three questions: what, why, and how?
猜你喜欢
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Why can't you remember when reading? Why can't you remember- My technology learning methodology
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
QT designer plug-in implementation of QT plug-in
6月书讯 | 9本新书上市,阵容强大,闭眼入!
pip 安装第三方库
C language: examples of logical operation and judgment selection structure
随机推荐
【提高课】ST表解决区间最值问题【2】
Playing with concurrency: what are the ways of communication between threads?
Pytoch --- use pytoch to realize u-net semantic segmentation
pip 安装第三方库
How to solve the code error when storing array data into the database
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
[JS event -- event flow]
Go variables and constants
【c语言】动态规划---入门到起立
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
PIP installation of third-party libraries
go 分支与循环
CorelDRAW graphics suite2022 free graphic design software
云服务器的安全设置常识
Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Yyds dry inventory compiler and compiler tools
How much is the tuition fee of SCM training class? How long is the study time?
Binary tree problem solving (2)
Raspberry pie GPIO pin controls traffic light and buzzer
Homework of the 16th week