当前位置:网站首页>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. !!!
边栏推荐
- 【c语言】动态规划---入门到起立
- Social media search engine optimization and its importance
- Shutdown procedure after 60
- Pytorch-Yolov5从0运行Bug解决:
- win11安装pytorch-gpu遇到的坑
- cs架构下抓包的几种方法
- Go branch and loop
- C language practice - number guessing game
- Playing with concurrency: what are the ways of communication between threads?
- Homework of the 16th week
猜你喜欢

win11安装pytorch-gpu遇到的坑

Common sense of cloud server security settings

Fluent icon demo

How much is the tuition fee of SCM training class? How long is the study time?

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

Target free or target specific: a simple and effective zero sample position detection comparative learning method

Deep understanding of lambda expressions

Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial

阿里云polkit pkexec 本地提权漏洞

My first experience of shadowless cloud computer
随机推荐
Playing with concurrency: what are the ways of communication between threads?
【c语言】动态规划---入门到起立
Pytorch---使用Pytorch进行鸟类的预测
office_ Delete the last page of word (the seemingly blank page)
10 minutes to understand CMS garbage collector in JVM
The confusion I encountered when learning stm32
win11安装pytorch-gpu遇到的坑
Pytoch --- use pytoch to realize u-net semantic segmentation
Ognl和EL表达式以及内存马的安全研究
Today's plan: February 15, 2022
What methods should service define?
Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
IDEA xml中sql没提示,且方言设置没用。
Read "the way to clean code" - function names should express their behavior
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Target free or target specific: a simple and effective zero sample position detection comparative learning method
Installation and use of blue lake
Go language naming specification
First acquaintance with P4 language
C语言猜数字游戏

