amazon oa2 - insert a value into a cycled linked list
2016-01-18 13:09
417 查看
遍历,一共有三种情况,
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;
}
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;
}
相关文章推荐
- iOS9之后支付宝的回调以及iOS8之后提示框UIAlertController
- UILabel设置富文本格式显示
- 在线安装maven插件问题:Cannot complete the install because one or more required items could not be found.
- 素材收集整理
- UICollectionView瀑布流的实现,带例子
- The server quit without updating PID file (mysql.pid)一次意外mysql停止运行备忘录
- UILabel 简单的富文本
- 如何通过代理之道UItableView上cell上面的button点击的是哪一行的
- java_easyui体系之DataGrid(4)[转]
- iOS 隐藏单个页面的UITabBar
- 微软COM中GUID和UUID、CLSID、IID
- java_easyui体系之DataGrid(3)[转]
- resque VS sidekiq & AMQP - RabbitMQ 消息处理
- easyui 页签
- Mockplus原型交互跟我做之3 - 认识“链接点”
- APUE之fork两次与僵尸进程的问题
- UITextView 限制输入文本长度
- Mockplus原型交互跟我做之2- 旋转的风车
- Mockplus原型交互跟我做之1 - 30秒做一个自动消失的消息框(Toast)
- chain.doFilter(request,response)含义