当前位置:网站首页>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
边栏推荐
- [pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)
- CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
- IM即时通讯开发优化提升连接成功率、速度等
- CRC16 data verification supports modelbus and XMODEM verification modes (C language)
- IM即时通讯软件开发网络请求成功率的优化
- Multiple commands produce '... /xxx.app/assets.car' problem
- Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
- 一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
- Design direction of daily development plan
- Multiple commands produce ‘.../xxx.app/Assets.car‘问题
猜你喜欢

nowcode-学会删除链表中重复元素两题(详解)

The epidemic dividend disappeared, and the "home fitness" foam dissipated

魏建军骑宝马也追不上李书福

Ansa secondary development - build ansa secondary development environment on Visual Studio code

500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued

Sort 1-insert sort and Hill sort

ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

排序1-插入排序与希尔排序

Leetcode topic
随机推荐
每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
Configure HyperMesh secondary development environment on vs Code
asp.net大文件分块上传断点续传demo
Ansa secondary development - build ansa/meta secondary development environment on pycharm
PHP 图片上传
UNP前六章 回射服务模型 解析
Stm32cube infrared remote control: input capture
魏建军骑宝马也追不上李书福
SCI scientific paper writing Growth Camp (full version)
IT远程运维是什么意思?远程运维软件哪个好?
Sort 5-count sort
Automatic conversion and cast
el-input限制只能输入规定的数
LwIP development | socket | UDP
ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境
8051 series MCU firmware upgrade IAP
nowcode-学会删除链表中重复元素两题(详解)
Solve the width overflow of rich text pictures such as uniapp
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
Sort 4-heap sort and massive TOPK problem