遍历,一共有三种情况,

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. enable feature AJAX of MOSS2007

    As default, the feature AJAX of MOSS2007 is disabled, so the site web configuration file should be m ...

  2. mysql 批量插入数据存储过程

    create procedure pFastCreateNums (cnt int unsigned) begin declare s int unsigned default 1; truncate ...

  3. yii2 的request get pos请求 基本用法示例

    yii2好久没用了, 基本的都快忘了,赶紧记录一下. 1.普通的get和pst请求 $request = Yii::$app->request; $get = $request->get( ...

  4. sqlserver行列转换问题(网上搜集)

    (列->行) 一.FOR XML PATH 简单介绍              那么还是首先来介绍一下FOR XML PATH ,假设现在有一张兴趣爱好表(hobby)用来存放兴趣爱好,表结构如 ...

  5. 【Linux】【通信】1.ping不通

    关于为什么ping不通有很多种原因,但直接的表象就网络之间没有成功进行通讯: 在构建虚拟机和win之间的交互时,主要使用了3种网络模式: 桥接bridge VMnet0 主机host     VMne ...

  6. Android apk集成

    刚开始学习Android,对很多东西都不懂,所以以下是我做的第一件事,记录一下,也就是apk的集成: 我们集成的apk是已经签过名的第三方apk,并且需要集成到system/priv-app目录下,过 ...

  7. 1476. Lunar Code

    http://acm.timus.ru/problem.aspx?space=1&num=1476 由于前一列对后一列有影响,所以需要保持前一列的状态, 但无需用状态压缩来保存(也保存不了) ...

  8. Qt中2D绘图问题总结(二)----------坐标系统

    坐标系统 使用QPainter绘制时使用到逻辑坐标,然后转换成绘图设备的物理坐标. 逻辑坐标到物理坐标的映射由QPainter的worldTransform()函数.QPainter的viewport ...

  9. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  10. Android ListView加载更多

    先看效果: ListView的footer布局: <?xml version="1.0" encoding="utf-8"?> <Relati ...