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 ...
随机推荐
- 30 天精通 Git 版本控管
https://github.com/doggy8088/Learn-Git-in-30-days
- ORACLE 自定义聚合函数
用户可以自定义聚合函数 ODCIAggregate,定义了四个聚集函数:初始化.迭代.合并和终止. Initialization is accomplished by the ODCIAggrega ...
- ExtJs 进度条(轮询)
客户端代码: Ext.onReady(function () { Ext.get('mb').on('click', function () { Ext.Ajax.request({ url: roo ...
- socket.io与redis构建实时推送
准备工作 1:准备nodejs环境 2:使用npm工具安装 socket.io npm install socket.io 3:下载客户端socket.io文件 解压附件中的文件 node app. ...
- IO多路复用,实现多线程监听
服务端 import socket sk1 = socket.socket() sk1.bind(('127.0.0.1',8001)) sk1.listen() sk2 = socket.socke ...
- Topcoder SRM558 1000 SurroundingGame
题意:给定一个网格,每个网格有选取代价和占据收益.每个点被占据,需要满足以下两个条件至少一个条件:1.被选取 2.邻近方格都被选取(有公共边被称为邻近) 不一定要占据所有方格,求最大收益. 第一直 ...
- 【DIY】【外壳】木板 & 亚克力 加工
—————————————————————————————————————————————————————————————————————— 一.途径 淘宝 https://item.taobao.c ...
- Eclipse 常用快捷键
Ctrl + Shift + L : 可以查看快捷键方式 Ctrl + 1 : 快速修复 Ctrl + / : 注释当前行 Ctrl + D : 删除当前行 Shift + Enter : 可以在当 ...
- spring IOC装配Bean(注解方式)
1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...
- 说一说windows原生docker及windows Server Container , Hyper Container 之间的关系(学习总结)
前一段时间学习netcore的时候解除到了docker,感觉真是不错的技术.百度了不少教程.因为我用windows就下载安装了一下试试.但是没有安装成功,才发现 需要安装virtualbox虚拟机,与 ...