当前位置:网站首页>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. !!!
边栏推荐
- Pytorch-Yolov5从0运行Bug解决:
- Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
- Which insurance company has a better product of anti-cancer insurance?
- Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
- Target free or target specific: a simple and effective zero sample position detection comparative learning method
- 万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
- LCM of Spreadtrum platform rotates 180 °
- [JS -- map string]
- Major domestic quantitative trading platforms
- PIP installation of third-party libraries
猜你喜欢
Delete the code you wrote? Sentenced to 10 months!
Realizing deep learning framework from zero -- Introduction to neural network
Force buckle 540 A single element in an ordered array
MySQL advanced SQL statement 2
初识P4语言
10 minutes to understand CMS garbage collector in JVM
Www2022 | know your way back: self training method of graph neural network under distribution and migration
[JS event -- event flow]
Landing guide for "prohibit using select * as query field list"
【c语言】基础篇学习笔记
随机推荐
Shutdown procedure after 60
Wechat applet JWT login issue token
Message mechanism -- message processing
Websites that it people often visit
Pytorch-Yolov5从0运行Bug解决:
cookie、session、tooken
There is no prompt for SQL in idea XML, and the dialect setting is useless.
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
cookie、session、tooken
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
C language practice - number guessing game
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
【c语言】动态规划---入门到起立
Delete the code you wrote? Sentenced to 10 months!
FAQ | FAQ for building applications for large screen devices
Alibaba cloud polkit pkexec local rights lifting vulnerability
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
如何解决在editor模式下 无法删除物体的问题
Read "the way to clean code" - function names should express their behavior
CorelDRAW graphics suite2022 free graphic design software