当前位置:网站首页>[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;
}
};
边栏推荐
- ESP8266连接onenet(旧版MQTT方式)
- Keyword inline (inline function) usage analysis [C language]
- JS数组常用方法的分类、理解和运用
- Redis based distributed locks and ultra detailed improvement ideas
- Redis cache update strategy, cache penetration, avalanche, breakdown problems
- Common DOS commands
- Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
- JS object and event learning notes
- Générateur d'identification distribué basé sur redis
- OSPF message details - LSA overview
猜你喜欢
Page performance optimization of video scene
Pytorch four commonly used optimizer tests
Arduino uno R3 register writing method (1) -- pin level state change
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
I2C bus timing explanation
open-mmlab labelImg mmdetection
The dolphin scheduler remotely executes shell scripts through the expect command
JS變量類型以及常用類型轉換
Characteristics, task status and startup of UCOS III
随机推荐
[Red Treasure Book Notes simplified version] Chapter 12 BOM
荣耀Magic 3Pro 充电架构分析
AMBA、AHB、APB、AXI的理解
I2C bus timing explanation
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
JS正则表达式基础知识学习
Pytorch four commonly used optimizer tests
Important methods of array and string
OPPO VOOC快充电路和协议
Imgcat usage experience
GCC compilation options
History object
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Flink late data processing (3)
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
Inline detailed explanation [C language]
ES6语法总结--上篇(基础篇)
(五)R语言入门生物信息学——ORF和序列分析
高通&MTK&麒麟 手機平臺USB3.0方案對比
STM32 how to locate the code segment that causes hard fault