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的更多相关文章

  1. LeetCode 708. Insert into a Cyclic Sorted List

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

  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. 自定义PlantUML和C4 Model样式

    什么是PlantUml PlantUml是一个支持快速绘制的开源项目.其定义了一套完整的语言用于实现UML关系图的描述.并基于强大的graphviz图形渲染库进行UML图的生成.绘制的UML图还可以导 ...

  2. 用户管理和su,id 命令

    useradd userdel usermod groupadd groupdel 用户管理 为什么需要有用户? 1. linux是一个多用户系统 2. 权限管理(权限最小化) 用户:存在的目录是为了 ...

  3. Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门

    服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...

  4. js 复制(自动)

    <script type="text/javascript"> document.writeln("<input type=\'text\' id=\' ...

  5. [jzoj]2538.【NOIP2009TG】Hankson 的趣味题

    Link https://jzoj.net/senior/#main/show/2538 Description Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫H ...

  6. [jzoj]4216.【NOIP2015模拟9.12】平方和

    Link https://jzoj.net/senior/#main/show/4216 Description 给出一个N个整数构成的序列,有M次操作,每次操作有一下三种: ①Insert Y X, ...

  7. 使用boost.python封装C++库

    使用boost.python封装C++库 C++以高性能著称,但是编写较为复杂.而简洁是Python的强项.如果能珠联璧合,就能发挥两家之长.本文尝试用boost库的python模块封装C++ 前期准 ...

  8. Linux下的文件切割和文件合并

    linux下文件分割可以通过split命令来实现,可以指定按行数分割和按大小分割两种模式.Linux下文件合并可以通过cat命令来实现. 在Linux下用split进行文件分割: ①:指定分割后文件行 ...

  9. (66)Wangdao.com第十一天_JavaScript 数组Array

    数组 Array 本质上,数组属于一种特殊的对象.typeof 运算符会返回数组的类型是 object 数组的特殊性体现在,它的键名是按次序排列的一组整数(0,1,2...) // Object.ke ...

  10. JS-原型的某些概念

    prototype:构造函数拥有一个对象,称为构造函数的原型属性,可以通过 构造函数prototype进行访问. __proto__: 构造函数所创造出的实例对象,可通过该属性访问原型对象. cons ...