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的更多相关文章

  1. 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 ...

  2. LeetCode 708. Insert into a Cyclic Sorted List

    原题链接在这里:https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/ 题目: Given a node from a cycl ...

  3. [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 ...

  4. 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 ...

  5. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  6. 九章lintcode作业题

    1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...

  7. 算法与数据结构基础 - 链表(Linked List)

    链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...

  8. [LeetCode] 147. Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. C语言入门(12)——递归

    一个函数在它的函数体内调用它自身称为递归调用.有递归调用操作的函数被称为递归函数.递归调用可以是直接调用,也可以是间接调用.也可以理解为函数的嵌套调用是函数本身. 例如实现一个求阶乘的函数: long ...

  2. nodejs项目中的路由写法

    //两种路由写法,一种封装成函数,返回结果,此种方法可以传递参数, "use strict"; var _ = require("lodash"); var e ...

  3. [置顶] How to compile openjdk 7 in RHEL5

    1. 为什么要编译openjdk的code? 因为从Eclipse调试JDK的代码时,方法中的局部变量不能显示,这样是因为编译JDK时,没有编译成debug版本. 2. RHEL5中自带的开发者JDK ...

  4. bootstrap 响应式布局 居中问题

    大家能够通过table来设置居中: display: table; width: auto; margin-left: auto; margin-right: auto;

  5. C++ - Vector 计算 均值(mean) 和 方差(variance)

    Vector 计算 均值(mean) 和 方差(variance) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24623187 ...

  6. Android牛博

    Android实现伸缩弹力分布菜单效果 摘要: 本文介绍下在Android中实现伸缩弹力分布菜单效果.关于这种菜单效果在IPhone中比较常见,效果比较酷.那么在Android中实现只是一种简单的模仿 ...

  7. Extjs 4 生成饼状图的例子

    前台: //远程抄表设备下落图表数据 var Store1 = new Ext.data.Store({ <span style="white-space:pre"> ...

  8. Swift学习之十四:闭包(Closures)

    * 闭包(Closures) * 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. * 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambdas ...

  9. WCF契约之---服务契约 、数据契约、 消息契约

    本篇博文只是简单说下WCF中的契约的种类.作用以及一些简单的代码示例.在WCF中契约分为服务契约.数据契约和消息契约.下面对这几种契约进行简单的介绍. 服务契约 服务契约描述了暴露给外部的类型(接口或 ...

  10. SQL Server 查看数据表占用空间大小的SQL语句

    ) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused ...