当前位置:网站首页>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)
本篇完!!!
边栏推荐
- uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- Pytorch-Yolov5从0运行Bug解决:
- CorelDRAW Graphics Suite2022免费图形设计软件
- Go function
- 藍湖的安裝及使用
- How much can a job hopping increase? Today, I saw the ceiling of job hopping.
- Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
- Ognl和EL表达式以及内存马的安全研究
- One click generation and conversion of markdown directory to word format
- QT designer plug-in implementation of QT plug-in
猜你喜欢

Why can't you remember when reading? Why can't you remember- My technology learning methodology

"No war on the Western Front" we just began to love life, but we had to shoot at everything

PR zero foundation introductory guide note 2

Pytorch---使用Pytorch进行鸟类的预测

Document declaration and character encoding

Raspberry pie GPIO pin controls traffic light and buzzer

Pytoch --- use pytoch for image positioning

Opencv learning example code 3.2.4 LUT

Free drawing software recommended - draw io

MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
随机推荐
pip 安装第三方库
QT designer plug-in implementation of QT plug-in
Thinkphp6 limit interface access frequency
Shutdown procedure after 60
LCM of Spreadtrum platform rotates 180 °
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
二叉树解题(二)
Binary tree problem solving (1)
Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
Message mechanism -- message processing
MySQL advanced SQL statement 2
Go语言介绍
Installation and use of blue lake
The confusion I encountered when learning stm32
[JS event -- event flow]
Wpviewpdf Delphi and Net PDF viewing component
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
Play with concurrency: what's the use of interruptedexception?
PIP installation of third-party libraries
Force buckle 540 A single element in an ordered array

