当前位置:网站首页>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)
本篇完!!!
边栏推荐
- [improvement class] st table to solve the interval maximum value problem [2]
- Why can't you remember when reading? Why can't you remember- My technology learning methodology
- Demonstration description of integrated base scheme
- Deep understanding of lambda expressions
- 【leetcode】34. Find the first and last positions of elements in a sorted array
- [untitled]
- Go language introduction
- Go function
- 万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
- [C language] basic learning notes
猜你喜欢

阿里云polkit pkexec 本地提权漏洞

Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000

What methods should service define?

【leetcode】34. Find the first and last positions of elements in a sorted array

Deep understanding of lambda expressions
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/e1/620443dbc6ea8b326e1242f25d6d74.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)

A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent

Common sense of cloud server security settings

CorelDRAW Graphics Suite2022免费图形设计软件
随机推荐
go 函数
【leetcode】34. Find the first and last positions of elements in a sorted array
Actual combat | use composite material 3 in application
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
千亿市场规模医疗美容行业的水究竟有多浑?
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
二叉树解题(一)
CorelDRAW Graphics Suite2022免费图形设计软件
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Document declaration and character encoding
Websites that it people often visit
pip 安装第三方库
手撕——排序
Go language introduction
How much is the tuition fee of SCM training class? How long is the study time?
Pytorch---使用Pytorch实现U-Net进行语义分割
PIP installation of third-party libraries
Today's plan: February 15, 2022
Okcc why is cloud call center better than traditional call center?
[improvement class] st table to solve the interval maximum value problem [2]

