当前位置:网站首页>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. !!!
边栏推荐
- Introduction to JSON usage scenarios and precautions
- Pytorch---使用Pytorch实现U-Net进行语义分割
- powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
- geotrust ov多域名ssl证书一年两千一百元包含几个域名?
- 【提高课】ST表解决区间最值问题【2】
- Mysql表insert中文变?号的问题解决办法
- Go language introduction
- Dare to go out for an interview without learning some distributed technology?
- Pytorch-Yolov5從0運行Bug解决:
- How to solve the problem that objects cannot be deleted in Editor Mode
猜你喜欢
随机推荐
记录一次Unity 2020.3.31f1的bug
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
Why can't you remember when reading? Why can't you remember- My technology learning methodology
Force buckle 540 A single element in an ordered array
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
Go function
Common sense of cloud server security settings
Play with concurrency: draw a thread state transition diagram
Wechat applet calculates the distance between the two places
Fluent icon demo
Mysql中常见的锁
Play with concurrency: what's the use of interruptedexception?
cs架构下抓包的几种方法
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
Dare to go out for an interview without learning some distributed technology?
Go branch and loop
First acquaintance with P4 language
[C language] Dynamic Planning --- from entry to standing up
Pytoch yolov5 runs bug solution from 0:
Document declaration and character encoding










