Insert into a Cyclic Sorted List
来源:互联网 发布:阿里云大厦 部门 编辑:程序博客网 时间:2024/05/28 22:12
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. Return the inserted new node.
public class Solution { /** * @param node a list node in the list * @param x an integer * @return the inserted new list node */ public ListNode insert(ListNode node, int x) { if (node == null) { node = new ListNode(x); node.next = node; return node; } ListNode point = node; ListNode prev = null; //循环一个周期 do { prev = point; point = point.next; if (prev.val <= x && point.val >= x) { break; } if ((prev.val > point.val) && (x > prev.val || x < point.val)) { break; } } while (point != node); ListNode newNode = new ListNode(x); newNode.next = point; prev.next = newNode; return newNode; }}
0 0
- Insert into a Cyclic Sorted List
- Insert into a Cyclic Sorted List
- Insert into a Cyclic Sorted List
- Insert into a Cyclic Sorted List
- Insert into a Cyclic Sorted List
- 【Hackerrank】Insert a node into a sorted doubly linked list
- Insert in a sorted list
- Insert into a sorted circular linked list 环状链表 插入节点
- Python pandas insert list into a cell
- demo for insert values into sorted single list
- Insert Data into Sorted Table
- Insert Data into Sorted Table
- Insert a node in a sorted linked list.
- P21 (*) Insert an element at a given position into a list.
- P20 (*) P21 (*)Remove the Kth element from a list;Insert an element at a given position into a list.
- jquery insert a table into div
- 代码面试题:Insert Sorted Linked List
- Sorted insert for circular linked list
- HDU1408 盐水的故事
- Group By两列字段时如何将组合的两个字段以表的形式给出
- 数据库与数据库管理系统
- Catch That Cow
- 2017.3.2
- Insert into a Cyclic Sorted List
- 关于Android开发界面设计之去掉标题栏的方法
- 51nod-1191 消灭兔子(贪心)
- 数据库和表结构得一些基本常用命令总结
- python中base64模块的加解密函数
- Insert into a Cyclic Sorted List
- CODEVS 1081 线段树练习 2
- Merge Two Sorted Lists
- JavaScript在JSF框架中汉字乱码问题的解决方法