[LeetCode] Insert into a Cyclic Sorted List 在循环有序的链表中插入结点
Given a node from a Circular Linked List which is sorted in ascending order, write a function to insert a value insertVal
into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list, and may not be necessarily the smallest value in the circular list.
If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the circular list should remain sorted.
If the list is empty (i.e., given node is null
), you should create a new single circular list and return the reference to that single node. Otherwise, you should return the original given node.
Example 1:
Input: head = [3,4,1], insertVal = 2
Output: [3,4,1,2]
Explanation: In the figure above, there is a sorted circular list of three elements. You are given a reference to the node with value 3, and we need to insert 2 into the list. The new node should be inserted between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3.
Example 2:
Input: head = [], insertVal = 1
Output: [1]
Explanation: The list is empty (given head isnull
). We create a new single circular list and return the reference to that single node.
Example 3:
Input: head = [1], insertVal = 0
Output: [1,0]
Constraints:
0 <= Number of Nodes <= 5 * 10^4
-10^6 <= Node.val <= 10^6
-10^6 <= insertVal <= 10^6
这道题让我们在循环有序的链表中插入结点,要求插入结点后,链表仍保持有序且循环。题目中强调了corner case的情况,就是当链表为空时,我们插入结点即要生成一个新的循环有序链表,那么我们可以先处理这个特殊情况,比较简单,就是新建一个结点,然后将next指针指向自己即可。好,下面来看给定的链表不为空的情况,最常见的情况就是要插入的结点值在两个有序结点值[a, b]之间,那么只要满足 a <= insertVal <= b 即可。由于是循环有序的链表,结点值不会一直上升,到某一个结点的时候,是最大值,但是下一个结点就是最小值了,就是题目中的例子,结点4到结点1的时候,就是下降了。那么在这个拐点的时候,插入值insertVal就会有两种特殊的情况,其大于等于最大值,或者小于等于最小值,比如插入值是5,或者0的时候,这两种情况都插入在结点4之后,可以放一起处理。而若其小于最大值,或者大于最小值,就是上面那种一般的情况,不会在这里处理,所以我们只要检测如果属于上面的两种情况之一,就break掉循环,进行插入结点处理即可,参见代码如下:
class Solution {
public:
Node* insert(Node* head, int insertVal) {
if (!head) {
head = new Node(insertVal, NULL);
head->next = head;
return head;
}
Node *pre = head, *cur = pre->next;
while (cur != head) {
if (pre->val <= insertVal && cur->val >= insertVal) break;
if (pre->val > cur->val && (pre->val <= insertVal || cur->val >= insertVal)) break;
pre = cur;
cur = cur->next;
}
pre->next = new Node(insertVal, cur);
return head;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Insert into a Cyclic Sorted List 在循环有序的链表中插入结点的更多相关文章
- LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)
题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description Problem :当前的数组 ...
- LeetCode OJ 之 Delete Node in a Linked List (删除链表中的结点)
题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...
- 【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II
[算法训练营day4]LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表 ...
- LeetCode 708. Insert into a Cyclic Sorted List
原题链接在这里:https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/ 题目: Given a node from a cycl ...
- Leetcode Articles: Insert into a Cyclic Sorted List
Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...
- Insert into a Cyclic Sorted List
Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
随机推荐
- dom4j,json,pattern性能对比【原】
报文大概2000字节,对比时为只取其中某个节点的值即可. 以下对比可知取少量节点时pattern性能是远大于dom4j,和json的, 但取大量的时候就不能这么以偏概全了. dom4j和pattern ...
- Nginx模块
模块概述 https://kb.cnblogs.com/page/98352/ Nginx模块工作原理概述 (Nginx本身支持多种模块,如HTTP模块.EVENT模块和MAIL模块,本文只讨论HTT ...
- -bash: Chmod: command not found
是增加该文件的所有者拥有运行权限 如果所有者是root ,还要加sudo chmod u+x drlinuxclient.bin (sudo) chmod u+x drlinuxclient.bin ...
- Ubuntu解决没有可安装候选软件包
解决方法:可以使用apt-cache search <package_name>寻找. 例如: E: 软件包 libqglviewer-dev 没有可安装候选 解决方法: apt-cach ...
- tcp协议的简单理解
tpc协议属于传输层协议,本篇主要介绍下几个概念,以及握手和挥手的过程. 1.tcp的几个概念 位码:即tcp标志位,有6种提示 SYN:synchronus,表示建立联机. ACK:acknowle ...
- Django 实现list页面检索
在list.html写入from表单 在views渲染list方法写入,从前台获取的searchtitle根据name实现检索
- webhook: requestbin
A Runscope Community Project — Learn more. RequestBin Bin URL Make a request to get started. After m ...
- windows上安装gcc/g++环境(MinGW,msys64等)
1 前言 经过折腾安装gcc环境,记录一下 2 工具 MinGW安装器: 下载地址1:MinGW官网 下载地址2:SourceForge (MinGW w64) 下载地址3:win-builds-1 ...
- web页面实现文件下载的几种方法
今天碰到文件下载的一些问题,本着知其然也要知其所以然的精神,站在巨人的肩膀上深入学习和测试了一下,抛砖引玉,现在总结结论如下: 1)标准URL下载方式可以通过在web页面中嵌入 url超级链接,标准的 ...
- 浮点数运算的精度问题:以js语言为例
在 JavaScript 中整数和浮点数都属于 Number 数据类型,所有数字都是以 64 位浮点数形式储存,即便整数也是如此. 所以我们在打印 1.00 这样的浮点数的结果是 1 而非 1.00 ...