当前位置:网站首页>[Offer29] 排序的循环链表
[Offer29] 排序的循环链表
2022-07-06 09:17:00 【劲腰傩舞】
题目概要:给你一个数,要求你把数据插入一个排序好的循环链表中,使得最终的链表升序。
大致思路:分清楚链表的三种情况:空链表、无最大最小值的链表、有最大最小值的链表。依次对三种情况进行处理。
题目
给定循环单调非递减列表中的一个点,写一个函数向这个列表中插入一个新元素 insertVal ,使这个列表仍然是循环升序的。
给定的可以是这个列表中任意一个顶点的指针,并不一定是这个列表中最小元素的指针。
如果有多个满足条件的插入位置,可以选择任意一个位置插入新的值,插入后整个列表仍然保持有序。
如果列表为空(给定的节点是 null),需要创建一个循环有序列表并返回这个节点。否则。请返回原先给定的节点。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/4ueAj6
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
输出示例
输入:head = [3,4,1], insertVal = 2
输出:[3,4,1,2]
输入:head = [], insertVal = 1
输出:[1]
解释:列表为空(给定的节点是 null),创建一个循环有序列表并返回这个节点。
输入:head = [1], insertVal = 0
输出:[1,0]
class Solution {
public:
Node* insert(Node* head, int insertVal) {
/*先处理空链表的情况*/
if (!head) {
/*只有一个结点的循环链表。指向自己*/
head = new Node(insertVal);
head->next = head;
return head;
}
/*使用另外一个指针进行遍历,因为最终还需要返回head*/
Node* cur = head;
/*循环条件用来排除链表中没有最大值最小值的情况:只有一个结点。或者所有结点的值相同*/
while(cur->next!=head){
/*并列情况一:插入值在前后两个数的大小之间*/
if (cur->val <= insertVal && insertVal<=cur->next->val)
break;
/*并列情况二:插入值大于链表最大值(第二个条件证明了链表中最大值的存在)*/
else if (cur->val <= insertVal && cur->next->val < cur->val)
break;
/*并列情况三:插入值小于链表最小值(第二个条件证明了链表中最小值的存在)*/
else if (cur->next->val >= insertVal && cur->next->val < cur->val)
break;
cur = cur->next;
}
/*循环完毕。在当前指针之后直接插入 *声明的结点就别用局部变量了。到函数之外就失效了*/
cur->next = new Node(insertVal, cur->next);
/*返回所要求的指针*/
return head;
}
};
边栏推荐
- [esp32 learning-2] esp32 address mapping
- Redis cache update strategy, cache penetration, avalanche, breakdown problems
- Arduino JSON data information parsing
- Arduino uno R3 register writing method (1) -- pin level state change
- 如何给Arduino项目添加音乐播放功能
- OPPO VOOC快充电路和协议
- Kconfig Kbuild
- Rough analysis of map file
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
- 记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
猜你喜欢

OPPO VOOC快充电路和协议

Common properties of location

Reno7 60W super flash charging architecture

Priority inversion and deadlock

STM32 how to locate the code segment that causes hard fault

ESP8266连接onenet(旧版MQTT方式)

JS regular expression basic knowledge learning

Kconfig Kbuild

Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation

【ESP32学习-1】Arduino ESP32开发环境搭建
随机推荐
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Cannot change version of project facet Dynamic Web Module to 2.3.
Oppo vooc fast charging circuit and protocol
MySQL占用内存过大解决方案
ES6 grammar summary -- Part I (basic)
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
I2C总线时序详解
【ESP32学习-2】esp32地址映射
JS变量类型以及常用类型转换
Pytorch: tensor operation (I) contiguous
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Basic knowledge of lithium battery
Pat 1097 duplication on a linked list (25 points)
Rough analysis of map file
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
Pytoch implements simple linear regression demo
open-mmlab labelImg mmdetection
Detailed explanation of Union [C language]
Classification, understanding and application of common methods of JS array
Symbolic representation of functions in deep learning papers