当前位置:网站首页>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. !!!
边栏推荐
- Markdown编辑语法
- cookie、session、tooken
- Message mechanism -- message processing
- Pytoch --- use pytoch to predict birds
- Force buckle 540 A single element in an ordered array
- LeetCode-对链表进行插入排序
- Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
- Alibaba cloud polkit pkexec local rights lifting vulnerability
- The solution to the complexity brought by lambda expression
- 手撕——排序
猜你喜欢

BGP experiment the next day

Use of go package
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up

Markdown编辑语法

Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知

LeetCode-对链表进行插入排序

Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!

Spring moves are coming. Watch the gods fight

Playing with concurrency: what are the ways of communication between threads?

ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
随机推荐
云服务器的安全设置常识
Wechat applet calculates the distance between the two places
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
Document declaration and character encoding
okcc为什么云呼叫中心比传统呼叫中心更好?
初识P4语言
Pytoch --- use pytoch to realize u-net semantic segmentation
Introduction to vmware workstation and vSphere
LeetCode-对链表进行插入排序
cookie、session、tooken
二叉树解题(二)
【c语言】动态规划---入门到起立
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
The confusion I encountered when learning stm32
Homework of the 16th week
Www 2022 | rethinking the knowledge map completion of graph convolution network
手撕——排序
BGP experiment the next day
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
