当前位置:网站首页>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)
本篇完!!!
边栏推荐
- [untitled]
- Sorted out an ECS summer money saving secret, this time @ old users come and take it away
- "No war on the Western Front" we just began to love life, but we had to shoot at everything
- Wechat applet calculates the distance between the two places
- Go language naming specification
- The original author is out! Faker. JS has been controlled by the community..
- How muddy is the water in the medical beauty industry with a market scale of 100 billion?
- C language practice - number guessing game
- How to solve the code error when storing array data into the database
- The core idea of performance optimization, dry goods sharing
猜你喜欢
CorelDRAW graphics suite2022 free graphic design software
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
C language practice - binary search (half search)
文档声明与字符编码
【c语言】基础篇学习笔记
QT designer plug-in implementation of QT plug-in
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
【c语言】动态规划---入门到起立
随机推荐
C language practice - binary search (half search)
阿里云polkit pkexec 本地提权漏洞
Go language introduction
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Raspberry pie GPIO pin controls traffic light and buzzer
缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
二叉树解题(二)
PIP installation of third-party libraries
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
LCM of Spreadtrum platform rotates 180 °
Major domestic quantitative trading platforms
MySQL advanced SQL statement 2
There is no prompt for SQL in idea XML, and the dialect setting is useless.
Hand tear - sort
powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
千亿市场规模医疗美容行业的水究竟有多浑?
【leetcode】81. Search rotation sort array II
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
Websites that it people often visit