当前位置:网站首页>Leetcode- insert and sort the linked list
Leetcode- insert and sort the linked list
2022-07-02 04:29:00 【Master_ hl】
Topic content

Sample analysis

Code implementation
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;
// Continue to judge the next node to be sorted
cur = SortedLast.next;
} else {
// Get into else It means to insert it in front
ListNode prev = newHead;
while(prev.next.val <= cur.val) {
prev = prev.next;
}
// The successor of the last data in the ordered sequence is bound to the next data to be sorted
SortedLast.next = cur.next;
// The successor of the inserted element is bound with the data after it in the ordered sequence
cur.next = prev.next;
// The subsequent data of the previous inserted element is bound to the inserted element
prev.next = cur;
// Continue to judge the next node to be sorted
cur = SortedLast.next;
}
}
return newHead.next;
}Analyze the connection with the arrangement
Key step analysis
When we adjust the order of nodes , First the SortedLast The next element to be sorted , The linked list has two fields , I'm used to tying the back first , Reprocess the successor of the previous element .
Analyze the above figure 1,2,3 Code for :
1. The successor of the last element of an ordered sequence is always bound to the first element of the sequence to be sorted , Cyclic advance else, Indicates that the value of the element to be inserted is less than SortedLast, So put SortedLast The successor of is bound to the next element to be inserted .
2. Whether you enter or not while loop ,prev The value of the position of must be just less than cur Value ,while The function of circulation is to adjust prev The point is just less than cur The previous node of the value of the node .
3. hold prev The successor tied the plug-in cur, To form an orderly sequence .
Complexity analysis
1. Time complexity
The sorting of linked lists is not like arrays , There is no need to move the data , Just change the direction , But because the linked list has no so-called subscript , So the time complexity required to find the subscript of the insertion position is O(n) ,n A sort of data , So the time complexity is O(n^2).
2. Spatial complexity O(1)
This is the end of this article. !!!
边栏推荐
- 深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
- Introduction to JSON usage scenarios and precautions
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- Deep understanding of lambda expressions
- MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
- Spring moves are coming. Watch the gods fight
- Major domestic quantitative trading platforms
- MySQL advanced SQL statement 2
- 缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
猜你喜欢

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

office_ Delete the last page of word (the seemingly blank page)

Document declaration and character encoding

Introduction to vmware workstation and vSphere

记录一次Unity 2020.3.31f1的bug

pip 安装第三方库

MySQL advanced SQL statement 2

66.qt quick-qml自定义日历组件(支持竖屏和横屏)
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/e3/fc2e78dc1e3e3cacbd1a389c82d33e.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

CorelDRAW graphics suite2022 free graphic design software
随机推荐
Common sense of cloud server security settings
BGP experiment the next day
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Why can't you remember when reading? Why can't you remember- My technology learning methodology
okcc为什么云呼叫中心比传统呼叫中心更好?
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Today's plan: February 15, 2022
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Landing guide for "prohibit using select * as query field list"
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
藍湖的安裝及使用
Introduction to JSON usage scenarios and precautions
Mysql中常见的锁
C language guessing numbers game
How to solve the problem that objects cannot be deleted in Editor Mode
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
【c语言】基础篇学习笔记
[improvement class] st table to solve the interval maximum value problem [2]
社交媒体搜索引擎优化及其重要性

