当前位置:网站首页>Leetcode刷题——对链表进行插入排序
Leetcode刷题——对链表进行插入排序
2022-08-05 05:17:00 【游辞】
题解:(想说的都在注释里)
struct ListNode* insertionSortList(struct ListNode* head){
if(head==NULL || head->next == NULL)
{
return head;//如果为空或者只有一个节点,则不需要排序
}
// 1、初始条件
struct ListNode* sortHead = head;//设置一个节点记录第一个节点
struct ListNode* cur = head->next;//设置一个cur连接原原头结点的下一节点
sortHead->next = NULL;//将第一第二个节点断开,成为两部分
while(cur) // 2、终止条件
{
//3、迭代条件
struct ListNode* next = cur->next;
//将cur节点插入前面的有序区间
struct ListNode* p = NULL, *c = sortHead;//用p记录前一节点,c记录当前位置
while(c)
{
if(cur->val < c->val) //插入值小于当前节点值,则开始插入
{
break; //将插入节点为最大值节点(用尾插)与正常情况结合处理
}
else //如果比较大于,则移动前后指针
{
p = c;
c = c->next;
}
}
//可获得三种节点地址
//1、NULL说明 插入节点值最小(用头插)
//2、节点c非空 说明插入值在链表中间
//3、节点c为空 说明插入节点值最大(用尾插)
if(p == NULL)//如果前驱节点为空,则用头插
{
cur->next = c;
sortHead = cur;
}
else //二三情况结合处理
{
p->next = cur;
cur->next = c;
}
//迭代cur当前节点
cur = next;
}
return sortHead;
}
边栏推荐
- Machine Learning (1) - Machine Learning Fundamentals
- LeetCode刷题之第1024题
- 二、自动配置之底层注解
- ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
- [Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
- 【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
- 《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
- 【ts】typeScript高阶:any和unknown
- Jupyter notebook选择不同的Anaconda环境作为内核运行
- LeetCode刷题之第530题
猜你喜欢
ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
SQL(1) - Add, delete, modify and search
You should write like this
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
HuiFer 带你读懂 BeanFactory getBean 方法
IJCAI 2022|边界引导的伪装目标检测模型BGNet
C语言—三子棋的实现
MSRA提出学习实例和分布式视觉表示的极端掩蔽模型ExtreMA
读论文- pix2pix
framebuffer应用编程及文字显示(1)
随机推荐
Comparison and summary of Tensorflow2 and Pytorch in terms of basic operations of tensor Tensor
Service
【nodejs】第一章:nodejs架构
SharedPreferences and SQlite database
深度学习系列(一)简介、线性回归与成本函数
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
LeetCode刷题之第530题
LeetCode刷题之第61题
PID详解
LeetCode刷题之第746题
原来何恺明提出的MAE还是一种数据增强
11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
浅谈遇到的小问题
关于使用QML的MediaPlayer实现视频和音频的播放时遇到的一些坑
PoE视频监控解决方案
CVPR 2022 | 70% memory savings, 2x faster training
读论文-Cycle GAN
Redis设计与实现(第一部分):数据结构与对象
idea 快速日志
tensorflow的session和内存溢出