LeetCode 708. Insert into a Cyclic Sorted List
原题链接在这里: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的更多相关文章
- 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] 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 ...
- 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 ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 【一天一道LeetCode】#83. Remove Duplicates from Sorted List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- B站动态转发抽奖脚本+教程
运行python脚本需要的条件: 1.连通的网络 2.已安装Python2并配置环境变量 3.Python脚本源码 环境搭建: 网络就不用我说了("'▽'") 那么下面我们来安装 ...
- docker的容器和镜像的清理
Docker用户会在使用docker一段时间后发现宿主机的磁盘很容易就快被占满,并且手动docker rmi [imgName]似乎并不能释放磁盘,貌似想删掉的镜像依然在宿主机中,下面针对这一问题提出 ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 18-22: ordinal not in range(128)
文件,放在site-package \Python27\Lib\site-packages sitecustomize.py # -*- coding: utf-8 -*- import sy ...
- [BZOJ5197] [CERC2017]Gambling Guide
[BZOJ5197] [CERC2017]Gambling Guide 题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5197 Solut ...
- 【华为敏捷/DevOps实践】7. 敏捷,DevOps,傻傻不分清楚【华为云技术分享】
文:姚冬(华为云DevCloud首席技术布道师,资深DevOps与精益/敏捷专家,金融解决方案技术Leader,中国DevOpsDays社区核心组织者) 前言 敏捷是什么?DevOps是什么?两者有什 ...
- PTA A1017
A1017 Queueing at Bank (25 分) 题目内容 Suppose a bank has K windows open for service. There is a yellow ...
- Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)
day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库: 简称:DataBase ---->DB 数据库即存放数据的仓库, ...
- Linux 软链接和硬链接简介
在Linux系统中,将文件分为两个部分:用户数据和元数据. 元数据(inode) 元数据即文件的索引节点(inode),用来记录文件的权限(r.w.x).文件的所有者和属组.文件的大小.文件的状态改变 ...
- springCloud学习5(Spring-Cloud-Stream事件驱动)
springcloud 总集:https://www.tapme.top/blog/detail/2019-02-28-11-33 代码见文章结尾 想想平常生活中做饭的场景,在用电饭锅做饭的同时, ...
- 编辑/etc/passwd文件进行权限升级的技巧
0x00 前言 在本文中,我们将学习“修改/etc/passwd文件以创建或更改用户的root权限的各种方法”.有时,一旦目标被攻击,就必须知道如何在/etc/passwd文件中编辑自己的用户以进行权 ...