当前位置:网站首页>Sorted circular linked list
Sorted circular linked list
2022-06-11 18:16:00 【AlbertOS】
introduce
Given Cyclic monotone non decreasing list A point in , Write a function to insert a new element into the list insertVal , Make the list still cyclic ascending .
Given can be the pointer to any vertex in the list , Not necessarily a pointer to the smallest element in the list .
If there are multiple insertion positions that meet the conditions , You can choose any location to insert a new value , After insertion, the whole list remains in order .
If the list is empty ( The given node is null), You need to create a circular sequence table and return this node . otherwise . Please return to the original given node .
Example

Input :head = [3,4,1], insertVal = 2
Output :[3,4,1,2]
explain : In the diagram above , There is a three element loop sequence table , You get a value of 3 Pointer to the node of , We need to insert elements into the table 2 . The newly inserted node should be in 1 and 3 Between , After insertion , The whole list is shown in the figure above , Finally, return to the node 3 .
solution (C++)
This problem is a pointer to any vertex , So the first idea is to traverse down , If you find a node cur Satisfy cur->val <= insertVal <= cur->next->val , You can insert it directly ;
If you can't find it , It means that all values are smaller or larger than it , So this will be inserted into the boundary position .
Satisfy insertVal <= cur->next->val <=cur->val( The insertion value is smaller than the smallest )
perhaps insertVal >= cur->val >= cur->next->val( The insertion value is larger than the maximum )
/* // Definition for a Node. class Node { public: int val; Node* next; Node() {} Node(int _val) { val = _val; next = NULL; } Node(int _val, Node* _next) { val = _val; next = _next; } }; */
class Solution {
public:
Node* insert(Node* head, int insertVal) {
// Sentenced to empty
if(head == nullptr){
head = new Node(insertVal);
head->next = head ;
return head ;
}
auto cur = head;
while(cur->next !=head){
// If the next node is smaller than the current node , Prove that the boundary position is reached ( The end boundary is the maximum )
if(cur->next->val < cur->val){
// If the next node is greater than the insertion value , Prove that the insertion value is smaller than the minimum
if(cur->next->val >= insertVal) break;
// If the current node is less than the insertion value , Prove that the insertion value is greater than the maximum value
else if(cur->val <= insertVal) break;
}
// If the current node is less than the insertion value and the next node is greater than the insertion value , Then insert it into this position
if(cur->val <= insertVal && cur->next->val >= insertVal)
break;
// If the three conditions are not met, find the next one
cur = cur->next;
}
cur->next = new Node(insertVal,cur->next);
return head;
}
};
边栏推荐
猜你喜欢

How ZABBIX can customize MySQL monitoring items and trigger alarms

Ctfhub SQL Boolean blind annotation

Line up to pick up the express. At this meeting, I sorted out all kinds of code sets

SISO Decoder for Repetition(补充章节4)

Explain AI accelerators in detail: GPU, DPU, IPU, TPU... There are infinite possibilities for AI acceleration schemes

神经网络与深度学习-2- 机器学习简单示例-PyTorch

Getting started with CTF
MySQL/Redis 常见面试题汇总

Use egg Js+mongodb simple implementation of curdapi

TR-069协议介绍
随机推荐
Radio button text background changes at the same time
Secret comment-----
【MapReduce】一个完整MR程序案例教你如何用IDEA打包及运行
Getting started with Wireshark
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
LDPC 7 - 解码简单例子
About element location and size
How to learn and self-study
Reading summary of nacos2.x source code
SISO Decoder for min-sum(补充章节2)
[C语言]用结构体把平均分和低于等于平均分的学生数据输出
关于keil中,while循环条件不成立却无法跳出的问题
Cryptology Summary
Spring 2021 daily question [end of week4]
Hello go (XV). Go language common standard library V
upload-labs通关未半而中道崩殂
[MapReduce] a complete Mr program case teaches you how to package and run with idea
SISO decoder for a general (n,n-1) SPC code(补充章节3)
System learning typescript (V) - joint type
【实用脚本】获取某个文件的行号,然后删除文件内容。
