Insert into a Cyclic Sorted List
Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the list. (increasing sorted)
We need to consider the following three cases:
1. pre->val <=x<=current->val:
insert between prev and current.
2. x is the maximum or minimum value in the list:
Insert before the head.
3. Traverses back to the starting point:
Insert before the starting point.
// aNode是引用,当aNode为空时,返回结点的指针
void insert(Node*& aNode, int x)
{
if (!aNode)
{
aNode = new Node(x);
aNode->next = aNode;
return;
} Node* p = aNode;
Node* prev = NULL;
do
{
prev = p;
p = p->next;
if (x <= p->data && x >= prev->data)
break;
if ((prev->data > p->data) && (x < p->data || x > pre->data))
break;
} while (p != aNode); Node* newNode = new Node(x);
newNode->next = p;
prev->next = newNode;
}
http://leetcode.com/2011/08/insert-into-a-cyclic-sorted-list.html
Insert into a Cyclic Sorted List的更多相关文章
- Leetcode Articles: Insert into a Cyclic Sorted List
Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...
- LeetCode 708. Insert into a Cyclic Sorted List
原题链接在这里:https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/ 题目: Given a node from a cycl ...
- [LeetCode] Insert into a Cyclic Sorted List 在循环有序的链表中插入结点
Given a node from a cyclic linked list which is sorted in ascending order, write a function to inser ...
- del|append()|insert()|pop()|remove()|sort()|sorted|reverse()|len()|range()|min()|max()|sum()|[:]|区分两种列表复制|
fruit = ['apple','banana','peach'] print fruit[0],fruit[-1] fruit_1 =[] fruit_1.append('orange') pri ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- java如何在函数中调用主函数的数组
import javax.swing.JOptionPane; public class Test { /** * @zdz */ public static void main(String[] a ...
- CC++初学者编程教程(1) Visual Stduio2010开发环境搭建
Visual Studio是微软公司推出的开发环境.是目前最流行的Windows平台应用程序开发环境. Visual Studio 2010版本于2010年4月12日上市,其集成开发环境(IDE)的界 ...
- PHPExcel 多工作表 导入
//参数初始化 $filePath = ''; if ($_FILES["file"]["error"] > 0) { returnJSON(ERROR_ ...
- ios开发之再谈设计模式
子曰:设计模式这东西,没有好坏之分,只有合适于不合适 天去面试很有意思,技术考官指着最后一道二选一的编程题说,这是昨天晚上专门为你新加的.当时我听后倍感惭愧. 虽然当时在纸上把大概思路和设计说了下.为 ...
- MySQL 5.6.19 二进制安装
1. 操作系统 CentOS release 6.2 (Final) 2. 创建用户和组 [root@mymaster1 ~]# groupadd mysql [root@ ...
- 初窥C++11:自己主动类型推导与类型获取
auto 话说C语言还处于K&R时代,也有auto a = 1;的写法.中文译过来叫自己主动变量.跟c++11的不同.C语言的auto a = 1;相当与 auto int a = 1;语句. ...
- 新辰:4G时代怎样利用手机进行移动APP营销?
未来的时代是4G时代,新辰手机用户的搜索量不在电脑端之下.那么,我们要怎样用手机进行营销呢?手机站点的竞价文章,要怎样去写比較好?手机站点要做专题吗?手机站点的优化思路在哪里?手机的系统不同,在不同的 ...
- TASKKILL命令使用方法
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM ima ...
- jquery 点击按钮实现listbox的显示与隐藏,点击其他地方按钮外的地方,隐藏listbox
本来不知道如何获取服务器的控件的,这下知道可以这么做了,所以记录下来.... <asp:ImageButton ID="alltime" ImageUrl="ima ...
- BOW
bag of words(NLP): 最初的Bag of words,也叫做"词袋",在信息检索中,Bag of words model假定对于一个文本,忽略其词序和语法,句法,将 ...