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 ...
随机推荐
- mysql timestamp 值不合法问题
Create Table: CREATE TABLE `RecruitmentDesc` ( `sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号(自增字段 ...
- OAuth认证的过程
在认证和授权的过程中涉及的三方包括: 服务提供方,用户使用服务提供方来存储受保护的资源,如照片,视频,联系人列表. 用户,存放在服务提供方的受保护的资源的拥有者. 客户端,要访 ...
- HDU 5727 Necklace(二分图匹配)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5727 [题目大意] 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,已知有些阴珠子和阳 ...
- .NET读取Project 2007 MPP项目文件
Project文件读取: 方法1:Microsoft.Project.OLEDB.11.0 string strConn = "Provider=Microsoft.Project.OLED ...
- CSS蒙版
蒙版:就是在图片上添加一个图层,用于美化页面,增加页面的可读性 <!DOCTYPE html><html><head lang="en"> &l ...
- 解决Adobe Acrobat “正在纠偏图像,正在旋转图像,正在分解页面”问题
笔者最近遇到的一个问题:用acrobat Pro X 打开pdf显示“正在纠偏图像,正在旋转图像,正在分解页面”,此时acrobat没有响应,要等待其完成,出现就得等一会儿,总出现,总得停顿,看一篇文 ...
- 获取 web容器中的bean
public class WebContextBeanFinder { public static Object getBean(String beanId) { ServletContext ser ...
- WinSock网络编程基础(1)
记录学习windows网络编程过程中遇到的问题和相关笔记 基本概念: Socket: socket起源于UNIX,Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.基于&qu ...
- Ie浏览器TextBox文本未居中
Ie浏览器TextBox文本未居中,而其他浏览器无问题时,可能原因是未设置垂直居中 vertical-align:middle
- Android 程序申请权限小知识点
在Google Play 应用商店,显示至少支持设备的数量时候会用到权限数量.其他地方用处不大. Android系统提供为程序提供了权限申请,即在manifest中使用uses-permission来 ...