当前位置:网站首页>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
边栏推荐
- Redis系列4:高可用之Sentinel(哨兵模式)
- Ansa secondary development - two methods of drawing the middle surface
- IM即时通讯开发优化提升连接成功率、速度等
- 排序3-选择排序与归并排序(递归实现+非递归实现)
- 日常开发方案设计指北
- Redis source code optimization -- binding core
- mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
- ANSA二次开发 - Apps和ANSA插件管理
- Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
- Two of C language programming!! Role of
猜你喜欢

Ansa secondary development - apps and ansa plug-in management

ANSA二次开发 - 抽中面的两种方法

Im im development optimization improves connection success rate, speed, etc

Huada chip hc32f4a0 realizes RS485 communication DMA transceiver

【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)

Ansa secondary development - two methods of drawing the middle surface

What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?

HM二次开发 - Data Names及其使用

栈的介绍与实现(详解)

优化Hypermesh脚本性能的几点建议
随机推荐
配置web服务器步骤详细记录(多有借鉴)
PHP image upload
IM即时通讯软件开发网络请求成功率的优化
PHP image synthesis technology
Rosen's QT journey 101 models and views in QT quick
Leetcode daily practice - 160. Cross linked list
Curl returns blank or null without output. Solve the problem
HyperMesh运行脚本文件的几种方法
HM secondary development - data names and its use
Sort 1-insert sort and Hill sort
Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
Qt学习之Qt Designer(设计师)
Ansa secondary development - build ansa secondary development environment on Visual Studio code
UNP前六章 回射服务模型 解析
LwIP development | realize TCP server through socket
WSL+Valgrind+Clion
HyperMesh auto save (enhanced) plug-in instructions
Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
信号屏蔽与处理