当前位置:网站首页>[sword offer ii] sword finger offer II 029 Sorted circular linked list
[sword offer ii] sword finger offer II 029 Sorted circular linked list
2022-06-27 21:56:00 【A Fei algorithm】
subject
The finger of the sword Offer II 029. Sorted circular linked list
A point in a monotone nondecreasing list of a given cycle , Write a function to insert a new element into the list insertVal , Make the list still cyclic ascending .
Given can be the pointer to any vertex in the list , Not necessarily a pointer to the smallest element in the list .
If there are multiple insertion positions that meet the conditions , You can choose any location to insert a new value , After insertion, the whole list remains in order .
If the list is empty ( The given node is null), You need to create a circular sequence table and return this node . otherwise . Please return to the original given node .
Example 1:
Input :head = [3,4,1], insertVal = 2
Output :[3,4,1,2]
explain : In the diagram above , There is a three element loop sequence table , You get a value of 3 Pointer to the node of , We need to insert elements into the table 2 . The newly inserted node should be in 1 and 3 Between , After insertion , The whole list is shown in the figure above , Finally, return to the node 3 .
Example 2:
Input :head = [], insertVal = 1
Output :[1]
explain : The list is empty. ( The given node is null), Create a circular sequence table and return this node .
Example 3:
Input :head = [1], insertVal = 0
Output :[1,0]
Tips :
0 <= Number of Nodes <= 5 * 10^4
-10^6 <= Node.val <= 10^6
-10^6 <= insertVal <= 10^6
Method 1: Traverse
- Find between two incrementing points
- Find the rotation point
public ListNode insert(ListNode head, int insertVal) {
if (head == null) {
ListNode insertNode = new ListNode(insertVal);
insertNode.next = insertNode;
return insertNode;
}
ListNode cur = head;
while (cur.next != head) {
if (cur.val < cur.next.val) {
if (insertVal >= cur.val && insertVal <= cur.next.val) {
insert(insertVal, cur);
return head;
}
}
if (cur.val > cur.next.val) {
if ((insertVal < cur.val && insertVal < cur.next.val) || insertVal > cur.val) {
insert(insertVal, cur);
return head;
}
}
cur = cur.next;
}
insert(insertVal, cur);
return head;
}
private static void insert(int insertVal, ListNode cur) {
ListNode insertNode = new ListNode(insertVal);
ListNode nxt = cur.next;
cur.next = insertNode;
insertNode.next = nxt;
}
边栏推荐
猜你喜欢

年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

跟我一起AQS SOS AQS

Go从入门到实战——错误机制(笔记)

The create database of gbase 8A takes a long time to query and is suspected to be stuck

Go从入门到实战——共享内存并发机制(笔记)

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear

Knowledge sorting of exception handling

BAT测试专家对web测试和APP测试的总结

Go从入门到实战——CSP并发机制(笔记)
随机推荐
Go从入门到实战——接口(笔记)
美团20k软件测试工程师的经验分享
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
数组作业题
Test automatique de Test logiciel - test d'interface de l'introduction à la maîtrise, apprendre un peu chaque jour
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
Go从入门到实战——协程机制(笔记)
Go从入门到实战——仅执行一次(笔记)
GBase 8a OLAP分析函数 cume_dist的使用样例
真香,自从用了Charles,Fiddler已经被我彻底卸载了
Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version
GBase 8a V8版本节点替换期间通过并发数控制资源使用减少对系统影响的方法
有时间看看ognl表达式
C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
Dynamic refresh mapper
Gbase 8A OLAP analysis function cume_ Example of dist
Go从入门到实战——任务的取消(笔记)
畅游动态规划之区间DP
Go from entry to practice -- CSP concurrency mechanism (note)
QT base64 encryption and decryption