当前位置:网站首页>剑指 Offer II 029. 排序的循环链表
剑指 Offer II 029. 排序的循环链表
2022-06-21 18:06:00 【SUNNY_CHANGQI】
The description of the problem
给定循环单调非递减列表中的一个点,写一个函数向这个列表中插入一个新元素 insertVal ,使这个列表仍然是循环升序的。
给定的可以是这个列表中任意一个顶点的指针,并不一定是这个列表中最小元素的指针。
如果有多个满足条件的插入位置,可以选择任意一个位置插入新的值,插入后整个列表仍然保持有序。
如果列表为空(给定的节点是 null),需要创建一个循环有序列表并返回这个节点。否则。请返回原先给定的节点。
来源:力扣(LeetCode)
an example
The codes for this
#include <iostream>
using namespace std;
// 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) {
// case 1 head == nullptr
Node *new_node = new Node(insertVal);
if (head == nullptr) {
new_node->next = new_node;
return new_node;
}
// case 2 there is only one element in the circular loop
if (head == head->next) {
head->next = new_node;
new_node->next = head;
return head;
}
// case 3 there are the elements in the circular loop more than 2
Node *cur_node = head, *next_node = head->next;
while(next_node != head){
if (insertVal >= cur_node->val &&insertVal <= next_node->val) break;
if ((cur_node->val > next_node->val) && (insertVal>= cur_node->val || insertVal <= next_node->val)){
break;
}
cur_node = next_node;
next_node = next_node->next;
}
cur_node->next = new_node;
new_node->next = next_node;
return head;
}
};
int main()
{
Solution s;
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = head;
int insertVal = 5;
head = s.insert(head, insertVal);
Node *cur_node = head;
cout << "insert " << insertVal << " to the list" << endl;
while (cur_node->next != head) {
cout << cur_node->val << " ";
cur_node = cur_node->next;
}
cout << endl;
return 0;
}
The corresponding results
$ ./test
insert 5 to the list
1 2 3 4
边栏推荐
- An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
- Two problems that may occur in the use of ThreadLocal and thread pool
- 在 KubeSphere 上部署 Apache Pulsar
- pnpm 中无法使用 patch-package 打补丁
- 298th weekly match
- GOF mode-03-behavioral mode (bottom)
- 【一起上水硕系列】Day One
- R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用summary函数查看模型汇总统计信息并解读特征
- MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级
- 《Go题库·9》同一个协程里面,对无缓冲channel同时发送和接收数据有什么问题
猜你喜欢

Gartner 网络研讨会 “九问数字化转型” 会后感

After the 80 version of Google browser, how to deal with the problem samesite cross domain problem

GoF模式-03-行为型模式(下)

How many correct answers can you get to Huawei Hongmeng certification test questions?

GOF mode-03-behavioral mode (bottom)

Medical expense list can be entered at a second speed, and OCR recognition can help double the efficiency

C语言刷题随记 —— 求 s=a+aa+aaa+aaaa+aa...a 的值

The main data products of China's two Fengyun meteorological "new stars" will be open and shared with global users

Huawei launches new products again? These models have excellent functions

在 KubeSphere 上部署 Apache Pulsar
随机推荐
Huawei launches new products again? These models have excellent functions
[high frequency interview questions] difficulty 1.5/5, classic "prefix and + two points" application questions
力扣每日一练之双指针1Day8
使用uniapp框架搭建浙里办微应用(单点登录、埋点、适老化、RPC网关)
【综合笔试题】难度 2.5/5 :「树状数组」与「双树状数组优化」
jvm造轮子
yolov5训练自己的数据集报错记录
C# Mapster 对象映射器学习
6月25日PMP考前指南,你需要做好这些
全国产加固以太网交换机选择技巧
第298场周赛
The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, uses the by parameter to specify the groupin
How to temporarily modify samesite=none and secure in Chrome browser
ThreadLocal与线程池在使用中可能会出现的两个问题
文献分析 Citespace 6.1.2 下载及安装教程
Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库
W10添加系统环境变量Path
MySQL的MVCC实现原理
医疗费用清单秒速录入,OCR识别助力效率倍增
The dist function of R language calculates the distance between two samples in dataframe data and returns the distance matrix between samples. The distance matrix is input to the hclust function for h