amazon oa2 - insert a value into a cycled linked list
遍历,一共有三种情况,
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的更多相关文章
- (链表) lintcode 219. Insert Node in Sorted Linked List
Description Insert a node in a sorted linked list. Example Example 1: Input: head = 1->4-> ...
- 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 ...
- 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 ...
- Trie树(转:http://blog.csdn.net/arhaiyun/article/details/11913501)
Trie 树, 又称字典树,单词查找树.它来源于retrieval(检索)中取中间四个字符构成(读音同try).用于存储大量的字符串以便支持快速模式匹配.主要应用在信息检索领域. Trie 有三种结构 ...
- C/C++ 笔试题
/////转自http://blog.csdn.net/suxinpingtao51/article/details/8015147#userconsent# 微软亚洲技术中心的面试题!!! 1.进程 ...
- [Algorithm Basics] Sorting, LinkedList
Time complexity: Binary search O(log2 n): i=0. n elements: ------------------- i=1. n/2 ...
- LeetCode Question Difficulty Distribution
参考链接:https://docs.google.com/spreadsheet/pub?key=0Aqt--%20wSNYfuxdGxQWVFsOGdVVWxQRlNUVXZTdEpOeEE& ...
- C/C++笔试题(很多)
微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...
- leetcode难度及面试频率
转载自:LeetCode Question Difficulty Distribution 1 Two Sum 2 5 array sort set ...
随机推荐
- Python模块学习笔记
1.作用域 私有private:用'_x'或'__xx'表示,如,_a,__ab; 公有public: 如 a,b; 特殊变量,可被直接引用,如:__author__,__name__,命名变量时一般 ...
- RPM
1.安装RPM 使用-ivh选项即可,能查看安装信息和进度. 例如: #RPM -ivh XXX.rpm RPM升级与更新,使用-Uvh选项或者-Fvh选项,两者略有区别. -Uvh选项:后面接的软件 ...
- UIImage
//设置UIImage的圆角 + (UIImage *)imageNamed:(NSString *)name size:(CGSize)size cornerRadius:(CGFloat)corn ...
- CentOS x64上Matlab R2015b的镜像安装方法与卸载
0. 原料 (1). CentOS_x64系统 CentOS 2.6.32-573.el6.x86_64 (2). Matlab R2015b_glnxa64.iso,可以从百度网盘下载到:链接: ...
- sql2008r2数据库附加的问题
sql2008r2数据库附加,一般都没有问题,但是偶尔也会出错,无法附加,一般的原因都是权限不够,主要是:Authenticated Users要开通完全控制功能,选中该用户(如果没有该用户,就添加) ...
- Java日志规范
前言 一个在生产环境里运行的程序如果没有日志是很让维护者提心吊胆的,有太多杂乱又无意义的日志也是令人伤神.程序出现问题时候,从日志里如果发现不了问题可能的原因是很令人受挫的.本文想讨论的是如何在Jav ...
- Ubuntu 修改hosts
Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改这个就可以了,结果发现是需要修改hosts.修改完之后要重启网络.具体过程 ...
- Redis并发锁控制
为了防止用户在页面上重复点击或者同时发起多次请求,请求处理需要操作redis缓存,这个时候需要对并发边界进行并发锁控制,实现思路: 由于每个页面发起的请求带的token具备唯一性,可以将token作为 ...
- sqlmap的学习以及使用
PDF.NET(PWMIS数据开发框架)之SQL-MAP目标和规范 将复杂查询写到SQL配置文件--SOD框架的SQL-MAP技术简介 PDF.NET数据开发框架 之SQL-MAP使用存储过程 不懂还 ...
- Graphics2D字符串根据文本框缩小字体自动换行
/** * *描述: 长字符串缩小字体自动换行 *@param g *@param text 字符串 *@param lineWidth 单元格宽度 *@param cellHeight 单元格高度 ...