当前位置:网站首页>剑指 Offer II 029. 排序的循环链表
剑指 Offer II 029. 排序的循环链表
2022-06-25 12:19:00 【小白码上飞】
一句话概要
注意各种边界问题,比如空链表,一个元素的链表,以及值都相等的链表
题目
给定循环单调非递减列表中的一个点,写一个函数向这个列表中插入一个新元素 insertVal ,使这个列表仍然是循环升序的。
给定的可以是这个列表中任意一个顶点的指针,并不一定是这个列表中最小元素的指针。
如果有多个满足条件的插入位置,可以选择任意一个位置插入新的值,插入后整个列表仍然保持有序。
如果列表为空(给定的节点是 null),需要创建一个循环有序列表并返回这个节点。否则。请返回原先给定的节点。

链接:https://leetcode.cn/problems/4ueAj6
思路
因为是单调非递减,所以沿着传入的头一直往前遍历即可。当然,遍历的时候要记住前一个节点。当给定的值大小在两个节点之间时,插入新节点即可。
经过各种调试,我发现这道题最难的地方,在于你要考虑多种特殊场景。比如给了一个空链表,给了一个只有一个元素的链表,给定的值不在链表最大最小值之间等等情况。简直爆炸。

解法
代码
public Node insert(Node head, int insertVal) {
if (head == null) {
head = new Node(insertVal);
head.next = head;
return head;
}
if (head.next == head) {
Node node = new Node(insertVal);
head.next = node;
node.next = head;
return head;
}
Node current = head.next;
Node pre = head;
boolean isRound = false;
while (true) {
if ((pre.val <= insertVal && current.val >= insertVal) ||
(pre.val > current.val && (pre.val < insertVal || current.val > insertVal))) {
Node node = new Node(insertVal);
pre.next = node;
node.next = current;
break;
}
// 跑了一圈都没有插入成功,说明这个链表的值都一样,因此随便插进去就行
if (pre == head) {
if (isRound) {
Node node = new Node(insertVal);
pre.next = node;
node.next = current;
break;
}
isRound = true;
}
current = current.next;
pre = pre.next;
}
return head;
}
边栏推荐
- Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection
- Micro engine generates QR code
- Embedded software development written examination and interview notes (latest update: February 17, 2022)
- The difference between this and super and their respective functions
- laravel 9
- Write regular isosceles triangle and inverse isosceles triangle with for loop in JS
- Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)
- Elemtnui select control combined with tree control to realize user-defined search method
- [AI helps scientific research] fool drawing of loss curve
- Singleton mode in PHP to reduce memory consumption
猜你喜欢
![[AI helps scientific research] fool drawing of loss curve](/img/38/5cb2a3d33a609dab3874215d5f7b5b.png)
[AI helps scientific research] fool drawing of loss curve
![[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1](/img/36/167397ce61240036c865dd99463f1b.jpg)
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1

2021-09-28

微信全文搜索技术优化

AI assisted paper drawing of PPT drawing

高性能负载均衡架构如何实现?

(7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)

冷启动的最优解决方案

Embedded software development written examination and interview notes (latest update: February 17, 2022)

Using CMD (command prompt) to install MySQL & configure the environment
随机推荐
Laravel multi project mutual access
STM32 在flash中存储float数据
JS function exercises
515. Find Largest Value in Each Tree Row
torch. Tensor splicing and list (tensors)
C program linking SQLSERVER database: instance failed
Initialize the project using the express framework
(6) Pyqt5--- > window jump (registration login function)
list.replace, str.append
mysql导入导出数据到excel表日期出现问题
(7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)
Flutter automatically disappears after receiving push
3+1 guarantee: how is the stability of the highly available system refined?
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1
Another night when visdom crashed
AI assisted paper drawing of PPT drawing
Visual studio2019 link opencv
Laravel echart statistical chart line chart
Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)
画图常用配色