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. 微软拼音转换工具类ChnCharInfo.dll

    1.简介 之前做汉字转拼音是使用各种枚举的方式,将各种情况列举,这种方式出错的机率很大,经常对不上号.(如果你想了解更多:http://www.cnblogs.com/islands/articles ...

  2. Android读写配置2

    上篇文章采用 Properties 读写配置,各种路径错误,要么没有写入权限. 后来查资料,采用另一种方式读写 SharedPreferencesImpl 直接贴代码 公共类 -- 读写 packag ...

  3. servlet(二):Servlet的web.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  4. Linux终端复用神器-Tmux使用梳理

    Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权.使用它最直观的好处就是,通过一个终端登录远程主机并运行tmux后,在其中可以开启多个控制台而无需再“ ...

  5. Beta(3/7)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  6. ECMA Script 6_对象的扩展

    对象 1. ES6 允许直接写入变量和函数,作为对象的属性和方法 const foo = 'bar'; /*****************属性的优化********************/ con ...

  7. Java课程课后作业190315之最大连续子数组(二维数组版)

    ,, 在本周的课堂上,老师再一次提高了要求,将一维数组升级成为了二维数组,然后求出块状的连续子数组. 一开始还想着借鉴之前球一维数组的O(n)的算法,后来还是没有找到头绪,舍友讲了自己的办法,但是没有 ...

  8. Oracle 11g修改字符集

    选择静默安装的安装字符集为默认的ZHS16GBK,工作中字符集为为AL32UTF8 一.登录oracle sqlplus / as sysdba shutdown immediate; STARTUP ...

  9. js中级6

    1.动画 (1)Css样式提供了运动 过渡属性transition  从一种情况到另一种情况叫过渡 transition:time          linear                 de ...

  10. [转]BT原理分析

    BitTorrent协议. BT全名为BitTorrent,是一个p2p软件,你在下载download的同时,也在为其他用户提供上传upload,因为大家是“互相帮助”,所以不会随着用户数的增加而降低 ...