当前位置:网站首页>Leetcode algorithm 147. insert and sort the linked list
Leetcode algorithm 147. insert and sort the linked list
2022-07-26 01:51:00 【Alex_ 12 hours a day 6 days a week】
Topic link :147. Insert and sort the linked list
Ideas
Algorithm : Insertion sort
data structure : Linked list
Ideas : Define a lastSorted The pointer points to the last ordered node , Let me define one more cur The pointer points to the node currently being judged , If the value of the last ordered node is less than the value of the node currently being judged , that lastSorted Pointers and cur Move the pointer back , Otherwise, it means that the current node needs to be inserted forward , So let's define a pre Node refers to the head node , If pre The next node value of is greater than cur Node value , Then we should put cur Insert into pre Just behind the node .
Code
C++
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (head == nullptr) {
return head;
}
ListNode *dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode *lastSorted = head, *cur = head->next;
while (cur != nullptr) {
if (lastSorted->val <= cur->val) {
lastSorted = lastSorted->next;
} else {
ListNode *pre = dummyHead;
while (pre->next->val <= cur->val) {
pre = pre->next;
}
lastSorted->next = cur->next;
cur->next = pre->next;
pre->next = cur;
}
cur = lastSorted->next;
}
return dummyHead->next;
}
};
边栏推荐
- MPLS知识点
- “蔚来杯“2022牛客暑期多校训练营2 H.[Take the Elevator] 维护线段
- Summary after reading "poor dad and rich dad"
- Create a future and enjoy extraordinary | gbase Nantah General Motors unveiled opengauss Developer Day 2022
- Infinite fraction path (BFS pruning)
- 快速创建题目文件夹
- 4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]
- Recommend a super good UI automation tool: uiautomator2!
- Quickly create a topic folder
- "Weilai Cup" 2022 Niuke summer multi school training camp 2 k.[link with bracket sequence i] bracket sequence DP
猜你喜欢

Implementation of recommendation system collaborative filtering in spark

Integer data type in C language (do you really understand it)

Worthington核酸酶、微球菌相关研究及测定方案

After reading this article, you should thoroughly understand how to do interface testing

flutter 下 grpc list没有Setter 方法 ,如何使用相关属性

Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation

P3166 number triangle (tolerance and exclusion +gcd)

Worthington产气荚膜梭菌神经氨酸酶的特征及测定

How to use the pagoda panel to deploy the full stack project of node to the server

Recommend a super good UI automation tool: uiautomator2!
随机推荐
劳驾问一下各位老师 oracle 到pg cdc oracle 那边字段大写 pg 这边小写 同
Integer data type in C language (do you really understand it)
[tips] what if you type with double quotation marks on the keyboard and the quotation marks disappear
The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
Network layer 2 and layer 3 forwarding
Prime Ring Problem
What is a test case? How to design?
4QAM、16QAM 调制与解调仿真电路,观察并分析QAM星座图和误码率曲线【matlab代码】
Move bricks (greedy perturbation + 01 backpack)
言语理解中心理解总结
[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]
The detailed knowledge summary of MySQL can be collected
Silicon Valley classroom - official account cloud on demand Silicon Valley classroom microservice project practical notes
IDEA如何快速删除最近打开的项目
Stack Title: basic calculator
How idea can quickly delete recently opened projects
E. Split into two sets
言语理解-片段阅读的结构剖析练习
IP address of the network
CPU的三种模式