原题链接在这里:https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/

题目:

Given a node from a cyclic linked list which is sorted in ascending order, write a function to insert a value into the list such that it remains a cyclic sorted 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 cyclic list.

If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the cyclic list should remain sorted.

If the list is empty (i.e., given node is null), you should create a new single cyclic list and return the reference to that single node. Otherwise, you should return the original given node.

The following example may help you understand the problem better:

In the figure above, there is a cyclic sorted 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 insert between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3.

题解:

上升阶段找比前大比后小的位置. 拐点看看insertVal是不是最大或者最小值, 是就加在拐点位置.

如果转回head还没有找到说明一直是平的, 就加在head前面.

Time Complexity: O(n).

Space: O(1).

AC Java:

 /*
// Definition for a Node.
class Node {
public int val;
public Node next; public Node() {} public Node(int _val,Node _next) {
val = _val;
next = _next;
}
};
*/
class Solution {
public Node insert(Node head, int insertVal) {
if(head == null){
head = new Node(insertVal, null);
head.next = head;
return head;
} Node cur = head;
while(true){
if(cur.val < cur.next.val){
// 上升阶段找比前小比后大的位置
if(cur.val<=insertVal && insertVal<=cur.next.val){
cur.next = new Node(insertVal, cur.next);
break;
}
}else if(cur.val > cur.next.val){
// 拐点,如果比前个还大大说明insertVal最大, 加在拐点位置
// 如果比后个还小说明insertVal最小, 也加在拐点位置
if(cur.val<=insertVal || insertVal<=cur.next.val){
cur.next = new Node(insertVal, cur.next);
break;
}
}else{
// 一直都是相等的点
if(cur.next == head){
cur.next = new Node(insertVal, head);
break;
}
} cur = cur.next;
} return head;
}
}

LeetCode 708. Insert into a Cyclic Sorted List的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [LeetCode] Insert into a Cyclic Sorted List 在循环有序的链表中插入结点

    Given a node from a cyclic linked list which is sorted in ascending order, write a function to inser ...

  4. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  5. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  6. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  7. 【一天一道LeetCode】#83. Remove Duplicates from Sorted List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  9. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

随机推荐

  1. Elasticsearch进阶篇(一)~head插件的安装与配置

    1.安装node.js 1.1.通过官网下载二进制安装包 https://nodejs.org/en/download/ 选择对应的版本,右键复制下载链接,进入linux目录,切换到要安装目录的磁盘. ...

  2. C语言return返回值深入理解

    C语言使用return关键字返回函数值,可以很好对函数做封装,此处的疑问是:函数内部创建的变量都是局部变量,即私有的,作用域就在函数之内,为什么却可以把值传给调用函数? 解释这个问题还需要从C语言调用 ...

  3. Akka-CQRS(9)- gRPC,实现前端设备与平台系统的高效集成

    前面我们完成了一个CQRS模式的数据采集(录入)平台.可以预见:数据的产生是在线下各式各样的终端系统中,包括web.桌面.移动终端.那么,为了实现一个完整的系统,必须把前端设备通过某种网络连接形式与数 ...

  4. Http、RESTful、RPC、MQ、Socket 概念与区别

    若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11700815.html 1. 关于HTTP: HTTP,即超文本传输协议,是一个 ...

  5. 解决使用RabbitTemplate操作RabbitMQ,发生The channelMax limit is reached. Try later.问题

    使用RabbitTemplate操纵RabbitMQ,每个RabbitTemplate等于一个connection,每个connection最多支持2048个channel,当hannel达到2048 ...

  6. C#类类型

    一.类和对象 假设我开了一家烤鱼店,每当客人来点餐时,我就会用笔和纸记录这笔订单,并计算出每单的价格.以下是记录的订单: 单号:00001种类:清江鱼口味:香辣配菜:豆腐价格:140元-------- ...

  7. C# vb .net实现宝丽来效果滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的宝丽来效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一 ...

  8. 重新学习Spring2——IOC和AOP原理彻底搞懂

    一.AOP 1 Spring AOP 的实现原理 是对OOP编程方式的一种补充.翻译过来为"面向切面编程". 1 AspectJ是静态代理的增强:所谓静态代理就是AOP框架会在便一 ...

  9. Java之路---Day10(抽象)

    2019-10-24-23:21:17 目录 1.抽象的方法 2.抽象类 3.抽象类和抽象方法的使用 4.抽象类的注意事项 5.案例代码 1.抽象的方法 What:如果父类当中的方法不确定如何进行{} ...

  10. 英语caement水泥

    水泥石  又称净浆硬化体.是指 硬化后的水泥浆体,称为水泥石,在英语里是cement有时写作caement [1] ,是由胶凝体.未水化的水泥颗粒内核.毛细孔等组 成的非均质体. 中文名:水泥石 外 ...