当前位置:网站首页>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)
本篇完!!!
边栏推荐
- Use a mask to restrict the input of the qlineedit control
- Pytoch yolov5 runs bug solution from 0:
- Mysql中常见的锁
- Wpviewpdf Delphi and Net PDF viewing component
- 60后关机程序
- Pytorch---使用Pytorch实现U-Net进行语义分割
- Spring moves are coming. Watch the gods fight
- Www2022 | know your way back: self training method of graph neural network under distribution and migration
- Wechat applet pull-down loading more waterfall flow loading
- My first experience of shadowless cloud computer
猜你喜欢

Pytoch --- use pytoch for image positioning

Déchirure à la main - tri

CorelDRAW Graphics Suite2022免费图形设计软件

QT designer plug-in implementation of QT plug-in
![[JS event -- event flow]](/img/fe/199890b082845f68b65f25056e6f29.jpg)
[JS event -- event flow]

What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?

【c语言】动态规划---入门到起立

Pytorch---使用Pytorch实现U-Net进行语义分割

Unit testing classic three questions: what, why, and how?

MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
随机推荐
Delete the code you wrote? Sentenced to 10 months!
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
BGP experiment the next day
Shutdown procedure after 60
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
Lei Jun wrote a blog when he was a programmer. It's awesome
First acquaintance with P4 language
The original author is out! Faker. JS has been controlled by the community..
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
Use a mask to restrict the input of the qlineedit control
Installation and use of blue lake
云服务器的安全设置常识
XSS prevention
CorelDRAW graphics suite2022 free graphic design software
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Thinkphp6 limit interface access frequency
6月书讯 | 9本新书上市,阵容强大,闭眼入!
LCM of Spreadtrum platform rotates 180 °
[untitled]
Common sense of cloud server security settings

