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. 从零开始学 iOS 开发的15条建议

    事情困难是事实,再困难的事还是要每天努力去做是更大的事实. 因为我是一路自学过来的,并且公认没什么天赋的前提下,进步得不算太慢,所以有很多打算从零开始的朋友会问我,该怎么学iOS开发.跟粉丝群的朋友交 ...

  2. rsyslog start with

    startswith Checks if the value is found exactly at the beginning of the property value. For example, ...

  3. 字符串匹配——Brute-Force 简单匹配算法

    下面几篇文章记录字符串匹配算法. Brute-Force算法简称BF算法,中文名叫简单匹配算法.正如其名,简单粗暴,按部就班地遍历所有字符,算法简单,效率低下,不被看好. 但也正因为不常用,反而容易生 ...

  4. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  5. nodejs开发微信1——微信access-token和tickets的数据模型

    /* jshint -W079 */ /* jshint -W020 */ "use strict"; //var _ = require("lodash"); ...

  6. Css 使div标签下沉到页面最低部

    .footer{ position:fixed; bottom:0; } 使用这两个标签就能达到效果

  7. jquery $.post 返回json数据

    $(function () { $("#prompt").hide(); $("#searchIpt").keyup(function () { var key ...

  8. spring拦截器的定义

    (一).拦截器的定义 1.为什么需要拦截器:在做身份认证或者是进行日志的记录时,我们需要通过拦截器达到我们的目的 2.什么事拦截器:在AOP(Aspect-Oriented Programming)中 ...

  9. 155Min Stack

    题目地址:155Min Stack 最近为了提高数据结构和算法能力,保证每天一到leetcode的题目.从easy开始,感觉这道题目还蛮好,记录一下. 题目大意:就是维护一个栈,获得栈中元素的的最小值 ...

  10. leetcode Container With Most Water python

    class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype ...