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

时间: 2024-11-02 23:33:29

Insert into a Cyclic Sorted List的相关文章

[LintCode] 599 Insert into a Cyclic Sorted List 解题报告

DescriptionGiven 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. Return the inserted new node. Noti

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

【leetcode】Search Insert Position

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

LeetCode: Search Insert Position 解题报告

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

LeetCode 035 Search Insert Position

题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5

Leetcode 题目整理 Sqrt &amp;&amp; Search Insert Position

Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt(7)这种情况,思路一就是应用二分法进行查找.每次给出中间值,然后比对cur的平方与目标值的大小.需要先设定两个变量用来存放左右游标. 这里要考虑一下整型溢出的问题,另外,即使不能开出整数的也要近似给出整数部分,不能忽略. 代码如下: int Solution::mySqrt(int x) { //

[Hackerrank]Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer, data, create a new Node object having data value data and insert it into a sorted linked list. 在双向升序链表中插入一个数,并且不能破环链表升序的结构: 思路:递归 /* Insert Node at the end of a linked list head po