遍历,一共有三种情况,

1. pre <= x <= current

2. 遍历到了head时,x>tail 或者 x<=head (不会等于tail)

3. 遍历回aNode或同值的Node,此时直接插到此Node的前面

public void insert(Node aNode, int x) {
  ListNode last = aNode;
  ListNode current = aNode.next;
  while (current.val != aNode.val) {
    if(last.val<=x && x<=current.val) {
      insertbtw(last,current,x);
      return;
    }
    else {
      if (last.val>current.val && (last.val<x || x<=current.val)) {
        insertbtw(last,current,x);
        return;
      }
    }
    last = current;
    current = current.next;
  }
  if(!inserted) {
    insertbtw(last,current,x);
  }
  return;
}

public void insertbtw(ListNode last, ListNode current, int x) {
  ListNode temp = new ListNode(x);
  last.next = temp;
  temp.next = current;

  return;

}

amazon oa2 - insert a value into a cycled linked list的更多相关文章

  1. (链表) lintcode 219. Insert Node in Sorted Linked List

    Description   Insert a node in a sorted linked list.   Example Example 1: Input: head = 1->4-> ...

  2. Insert Node in Sorted Linked List

    Insert a node in a sorted linked list. Have you met this question in a real interview?  Yes Example ...

  3. 219. Insert Node in Sorted Linked List【Naive】

    Insert a node in a sorted linked list. Example Given list = 1->4->6->8 and val = 5. Return  ...

  4. Trie树(转:http://blog.csdn.net/arhaiyun/article/details/11913501)

    Trie 树, 又称字典树,单词查找树.它来源于retrieval(检索)中取中间四个字符构成(读音同try).用于存储大量的字符串以便支持快速模式匹配.主要应用在信息检索领域. Trie 有三种结构 ...

  5. C/C++ 笔试题

    /////转自http://blog.csdn.net/suxinpingtao51/article/details/8015147#userconsent# 微软亚洲技术中心的面试题!!! 1.进程 ...

  6. [Algorithm Basics] Sorting, LinkedList

    Time complexity: Binary search O(log2 n): i=0.   n elements:         ------------------- i=1.   n/2 ...

  7. LeetCode Question Difficulty Distribution

    参考链接:https://docs.google.com/spreadsheet/pub?key=0Aqt--%20wSNYfuxdGxQWVFsOGdVVWxQRlNUVXZTdEpOeEE& ...

  8. C/C++笔试题(很多)

    微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...

  9. leetcode难度及面试频率

    转载自:LeetCode Question Difficulty Distribution                 1 Two Sum 2 5 array sort           set ...

随机推荐

  1. SQL 子查询,连接查询复习

    use lianxi0720 go --创建部门表 create table bumen ( bcode int primary key,--部门编号 bname ), --部门名称 bceo ), ...

  2. 第一章 UI实战开发 UIWindow UIView

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  3. C#获取并写入ORACLE数据库中中英文字符集问题

    背景: 开发语言:C# 开发工具:VS2010 A方ORACLE数据库:中文字符集 B方ORACLE数据库:英文字符集 传递方式:webservice方式(取数据,并把取出的数据放到DataTable ...

  4. The type String cannot be constructed. You must configure the container to supply this value.

    利用 Enterprise Library 5.0 Microsoft.Practices.EnterpriseLibrary.Common Microsoft.Practices.Enterpris ...

  5. jquery中链式调用原理

    (1).链式调用 $("#mybtn").css("width","100px") .css("height",&quo ...

  6. 通过npm安装 Cordova

    通过npm安装 Cordova 首先请确保你在本地已经安装了NodeJS(可以调用npm命令), 并且是联网状态的.如果不知道如何安装NodeJS, 请参考 ”四步完成NodeJS安装,配置和测试”. ...

  7. [深度优先搜索] POJ 1426 Find The Multiple

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28550   Accepted: 118 ...

  8. json转类

    JavaScriptSerializer js = new JavaScriptSerializer();T detaile = js.Deserialize<T>(json);

  9. eclipse 高亮代码

    本文整合自网络上的两种靠谱的使eclipse代码高亮的方式. 其实你可以在Window->proferences->java->editor->syndex coloring- ...

  10. codeforces 727F. Polycarp's problems

    题目链接:http://codeforces.com/contest/727/problem/F 题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该 ...