原题链接在这里: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. C++ 工程师养成 每日一题4.5 (迭代器遍历)

    首先说明,当每日一题标号不是整数时代表此题是较为简单的,我在这里整理一遍主要是我做错了(没错是我太菜各位大佬无视就好) 题目: 读入一个字符串str,输出字符串str中的连续最长的数字串 此题思路清晰 ...

  2. gtest入门

    介绍 gtest是谷歌开发的用来做C++单元测试的测试框架 基本概念 使用gtest,你就需要写断言(assertions),用来检查一个表达式是否为true.断言的结果有三个:正确.非致命错误.致命 ...

  3. DRF框架(三)——media资源路径设置、多表设计复习及补充、序列化组件(ModelSerializer)操作多表(序列化与反序列化)、多表序列化与反序列化整合(重点)

    media资源路径设置  (设置好后把图片放在这个文件夹中,通过链接能访问到图片) 1.先在根目录设置一个media文件夹 2.配置settings.py,加上下面的 MEDIA_URL = '/me ...

  4. CLRS10.2-7练习 - 翻转单向列表

    要求: Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of nelements. The pro ...

  5. Jmeter相关参数

    一.线程组 线程组主要包含三个参数:线程数.准备时长(Ramp-Up Period(in seconds)).循环次数. 线程数:虚拟用户数.一个虚拟用户占用一个进程或线程.设置多少虚拟用户数在这里也 ...

  6. Drools7 Hello Wrold 入门详细步骤--系列01课

    一.什么叫规则引擎?规则--->写在文档上引擎--->在java代码上,引用这个文档上的规则 二.drools规则引擎有什么用?简单来说就是将多变的规则,从业务代码中剥离出来(当规则变了之 ...

  7. 阿里巴巴 Java 开发手册 (九) 异常日志

    (一) 异常处理 1. [强制]Java 类库中定义的一类 RuntimeException 可以通过预先检查进行规避,而不应该 通过 catch 来处理,比如:IndexOutOfBoundsExc ...

  8. Oracle 12c数据库的安装

    Oracle一路走来,不知不觉中已经到了12c,最近要用这个新版本了,从oracle的官网上注册了个用户,下载了win版本64位的先试一下. (1)下载安装包 a. 到官网进行下载需要先注册一个Ora ...

  9. 【RAC】 RAC For W2K8R2 安装--dbca创建数据库(七)

    [RAC] RAC For W2K8R2 安装--dbca创建数据库(七) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可 ...

  10. centos8 网络配置

    目录 centos8已经发布了,下载了一个体验一下,新安装好的centos8默认网卡是没有启动的,安装好后需要先配置网络.在/etc/sysconfig/network-scripts目录下存放着网卡 ...