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 into the list such that it remains a cyclic sorted list. The given node can be any single node in the list.


Solution:
Basically, you would have a loop that traverse the cyclic sorted list and find the point where you insert the value (Let’s assume the value being inserted called x). You would only need to consider the following three cases:
1. prev→val ≤ x ≤ current→val:
Insert between prev and current.
2. x is the maximum or minimum value in the list:
- Insert before the head. (ie, the head has the smallest value and its prev→val > head→val.
3. Traverses back to the starting point:
- Insert before the starting point.
It's tricky to think of case 3:
Q: What if the list has only one value?A: Handled by case 3).
Q: What if the list is passed in as NULL?A: Then handle this special case by creating a new node pointing back to itself and return.
Q: What if the list contains all duplicates?A: Then it has been handled by case 3).
public class Solution {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
this.val = x;
this.next = this;
}
}
public ListNode rotateRight(ListNode start, int x) {
if (start == null) return new ListNode(x);
ListNode cur = start;
while (true) {
if (cur.val < cur.next.val) {//没有到拐点
if (x>=cur.val && x<=cur.next.val) {
insert(cur, x);
break;
}
else cur = cur.next;
}
else if (cur.val > cur.next.val) { //到了拐点
if (x>=cur.val || x<=cur.next.val) {
insert(cur, x);
break;
}
else cur = cur.next;
}
else { //cur.val == cur.next.val
if (cur.next == start) {
insert(cur, x);
break;
}
cur = cur.next;
}
}
return start;
}
public void insert(ListNode cur, int x) {
ListNode node = new ListNode(x);
node.next = cur.next;
cur.next = node;
}
}
Leetcode Articles: Insert into a Cyclic Sorted List的更多相关文章
- LeetCode 708. Insert into a Cyclic Sorted List
原题链接在这里:https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/ 题目: Given a node from a cycl ...
- 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 ...
随机推荐
- WPF中的逻辑树和可视化树
WPF中的逻辑树是指XAML元素级别的嵌套关系,逻辑树中的节点对应着XAML中的元素. 为了方便地自定义控件模板,WPF在逻辑树的基础上进一步细化,形成了一个“可视化树(Visual Tree)”,树 ...
- 在vue里添加好看的lottie动画 (^_^)
为什么用lottie ★~★ 1.能让你的程序不那么单调 2.能让一些有审美强迫症的同学好受一点 3.网上有丰富的资源 点我进入lottie资源网站 引入lottie库 (>.<) 在vu ...
- Servlet(九):web.xml文件和server.xml文件
Web.xml 文件使用总结:作用: 存储项目相关的配置信息,保护 Servlet.解耦一些数据对程序的依赖.使用位置: 每个 Web 项目中Tomcat 服务器中(在服务器目录 conf 目录中)区 ...
- 20172328 2018-2019《Java软件结构与数据结构》第七周学习总结
20172328 2018-2019<Java软件结构与数据结构>第七周学习总结 概述 Generalization 本周学习了第11章:二叉查找树.在本章中,主要探讨了二叉查找树的概念和 ...
- Linux生成私钥和公钥免密连接
本文介绍Linux系统生成私钥和公钥进行免密连接,内容比较简单,阅读需要3分钟. 1.大致流程 有时需要从服务器A免密连接到服务器B,这时需要在服务器A生成私钥和公钥,大致过程其实就2步. 1.1 在 ...
- 如何运行后台Service?
Unless you specify otherwise, most of the operations you do in an app run in the foreground on a spe ...
- ajax多图上传
百度云代码 参考:https://segmentfault.com/q/1010000004218827
- linux 文件属性(转)
1. 文件类型 - 普通文件 d 目录文件 l 链接文件 b 块设备文件 c 字符型设备文件 s socket文件 p 管道类型文件 块设备文件主要是指慢速设备,比如hd硬盘,数据主要是分块存储,所 ...
- Dreamweaver编辑区下方的属性栏显示
显示属性栏 不小心关闭了Dreamweaver的属性栏,突然用到之后不知道怎么显示,此时需要两步:选择[窗口]工具栏,选择[属性]选项. 此时又可以看到编辑区下方的属性栏了,而且出于编写代码的需要可以 ...
- JAVA新的一天
在2019/03/22/今天里,荣幸成为这个班级的一员,认识了新的小伙伴们,上午由老师大体说了一下java的理念,下午安装了DW编辑器,以及讲解了HTML的基本构造,和标签的使用,即使以前学习过,这次 ...