当前位置:网站首页>Leetcode learn to insert and sort unordered linked lists (detailed explanation)
Leetcode learn to insert and sort unordered linked lists (detailed explanation)
2022-07-28 16:43:00 【World_ living】
Catalog
Preface
Please have a seat
One 、 Insert the sorting question link into the unordered linked list
Title Description
Insert and sort the linked list .
Insertion sort algorithm :
- The insertion sort is iterative , Move only one element at a time , Until all elements can form an ordered output list .
- In each iteration , Insert sort removes only one element to be sorted from the input data , Find its place in the sequence , And insert it into .
- Repeat until all input data is inserted .
The test sample 1:
| Input : 4->2->1->3 |
|---|
| Output : 1->2->3->4 |
The test sample 2:
| Input : -1->5->3->4->0 |
|---|
| Output : -1->0->3->4->5 |
Topic ideas
Rearrange the list
- Remove the first node , And put the next empty , Prevent the formation of circular linked list
- Then define several pointers to operate , Compare c and cur
- If cur->val Greater than c->val, be c and p All in one , Otherwise start sorting
Pictured :
- One Always pay attention newhead->val Value , To compare newhead->val The value of and cur->val, If newhead->val<=cur->val, Then let newhead Point to cur, Guarantee newhead Always point to the head node . Two There's another way to write it , Observe the law , Only when p yes NULL It's time to change newhead, So it can be used p Is it NULL To change newhead.
Pictured :
- You have to judge p Is it NULL When , If the first comparison ,cur->val Less than c->val, be p No change required .
Be careful : Because it's out of order , So pay attention to newhead The direction of , To put newhead Always point to the head node .
There are two kinds of code , Different styles
Code 1
The code is as follows : This is the solution of not creating sentinel positions , Of course, this is to insert in the second loop
struct ListNode* insertionSortList(struct ListNode* head)
{
if(head==NULL||head->next==NULL)
{
return head;
}
//1
struct ListNode*newhead=head;
struct ListNode*cur=head->next;
newhead->next=NULL;
//2
while(cur)// Termination conditions
{
struct ListNode* next=cur->next;
struct ListNode* c=newhead;
struct ListNode* p=NULL;
while(c)
{
if(cur->val<=c->val)
{
// Determine the head node
if(newhead->val>=cur->val)
{
newhead=cur;
}
// Judge p
if(p!=NULL)
{
p->next=cur;
}
cur->next=c;
break;
}
else
{
p=c;
c=c->next;
}
}
if(c==NULL)
{
p->next=cur;
cur->next=c;
}
cur=next;
}
return newhead;
}
Code 2
The code is as follows : This is the solution to create sentinel positions , Of course, this is in the second loop to insert
typedef struct ListNode Node;
struct ListNode* insertionSortList(struct ListNode* head){
if(head == NULL || head->next == NULL)
return head;
// Create a sort linked list
Node* sortHead = (Node*)malloc(sizeof(Node));
// The head node is inserted first
sortHead->next = head;
head = head->next;
sortHead->next->next = NULL;
// Insert remaining nodes
Node* cur = head;
while(cur)
{
// First save next node
Node* next = cur->next;
// Start from the head of the sorting linked list , Find a suitable location for the node to be inserted
Node* sortPrev = sortHead;
Node* sortCur = sortHead->next;
while(sortCur)
{
if(cur->val > sortCur->val)
{
sortPrev = sortCur;
sortCur = sortCur->next;
}
else
{
break;
}
}
// Insert in place
sortPrev->next = cur;
cur->next = sortCur;
cur = next;
}
Node* sortList = sortHead->next;
free(sortHead);
return sortList;
}
summary
This question is to think comprehensively , Then write patiently and slowly
边栏推荐
- Thoughts on solving the pop-up of malicious computer advertisements
- 排序3-选择排序与归并排序(递归实现+非递归实现)
- ANSA二次开发 - 抽中面的两种方法
- Two of C language programming!! Role of
- Reentrant and non reentrant
- 2021-04-02
- 日常开发方案设计指北
- Qt学习之Qt Designer(设计师)
- Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
- Curl returns blank or null without output. Solve the problem
猜你喜欢

WSL+Valgrind+Clion

Ansa secondary development - apps and ansa plug-in management

Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code

Leetcode daily practice - 160. Cross linked list

排序4-堆排序与海量TopK问题

The little red book of accelerating investment, "rush to medical treatment"?

flashfxp 530 User cannot log in. ftp

HyperMesh auto save (enhanced) plug-in instructions

ANSA二次开发 - 界面开发工具介绍

Ansa secondary development - Introduction to interface development tools
随机推荐
Hdu1847 problem solving ideas
Multiple commands produce ‘.../xxx.app/Assets.car‘问题
PHP图片合成技术
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
LeetCode-学会对无序链表进行插入排序(详解)
PHP gets the applet code, and the applet jumps with parameters
The epidemic dividend disappeared, and the "home fitness" foam dissipated
LwIP development | socket | UDP
Analysis of echo service model in the first six chapters of unp
IM即时通讯开发优化提升连接成功率、速度等
Headline article_ signature
Leetcode daily practice - 160. Cross linked list
Qt学习之安装
Automatically pack compressed backup download and delete bat script commands
Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
HDU1847解题思路
IT远程运维是什么意思?远程运维软件哪个好?
redis源码优化--绑核
在vs code上配置Hypermesh二次开发环境
微软100题-天天做-第11题