当前位置:网站首页>Sword finger offer II 029 Sorted circular linked list
Sword finger offer II 029 Sorted circular linked list
2022-07-03 09:01:00 【Sasakihaise_】
The finger of the sword Offer II 029. Sorted circular linked list


【 analysis 】 Because the linked list is still in order after insertion , So we traverse the linked list , Find the first node larger than the element to be inserted , Just insert this element in front of this node , Add up to some details .
1. First, because I want to return head, So let's make a mark and save it head, Make Node dummy = head, then head When the cursor is used .
2. Because it needs to be inserted in front of a node , So you need the previous node of this node , Because only head, So we can't know head Of pre, So we put head When pre,head Of next Come and be head that will do .
3. But if we start with a much larger element than the one we want to insert , such as 5,7,0,2, Insert 1, We found that we ordered head 7( primary head5 Of next7) Already greater than 1 Of course. , however 1 It should not be inserted in 7 In front of . So in this case, we need to find the smallest head of the linked list , We found the head 0 One characteristic is his pre want >head, But it does not rule out 3,3,3,3 This situation , So we have to judge whether it is back dummy.
4. If the linked list is empty and there is only one node in the linked list, you need to pay extra attention .
Several special examples :
[3,3,5]
0
[1]
0
[1]
2
[3,3,3]
0
[]
1/*
// Definition for a Node.
class Node {
public int val;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _next) {
val = _val;
next = _next;
}
};
*/
class Solution {
public Node insert(Node head, int insertVal) {
if(head == null){
Node node = new Node(insertVal);
node.next = node;
return node;
}
Node dummy = head;
Node pre = dummy;
head = head.next;
if(insertVal < head.val){
while(pre.val <= head.val && head != dummy){
head = head.next; pre = pre.next;
}
}
while(true){
if(head.val > insertVal) break;
head = head.next;
pre = pre.next;
if(pre.val > head.val) break;
if(pre == dummy) break;
}
pre.next = new Node(insertVal, head);
return dummy;
}
}边栏推荐
- 剑指 Offer II 091. 粉刷房子
- Tree DP acwing 285 A dance without a boss
- LeetCode 513. 找树左下角的值
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- 常见渗透测试靶场
- Mortgage Calculator
- Monotonic stack -84 The largest rectangle in the histogram
- Arbre DP acwing 285. Un bal sans patron.
- LeetCode 324. 摆动排序 II
- Method of intercepting string in shell
猜你喜欢

剑指 Offer II 091. 粉刷房子

Notes and bugs generated during the use of h:i:s and y-m-d

LeetCode 57. 插入区间
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

分配异常的servlet

树形DP AcWing 285. 没有上司的舞会

Monotonic stack -84 The largest rectangle in the histogram

注解简化配置与启动时加载

JS ternary operator - learning notes (with cases)

状态压缩DP AcWing 91. 最短Hamilton路径
随机推荐
一个优秀速开发框架是什么样的?
Using variables in sed command
Notes and bugs generated during the use of h:i:s and y-m-d
JS ternary operator - learning notes (with cases)
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
LeetCode 871. 最低加油次数
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
Slice and index of array with data type
The method of replacing the newline character '\n' of a file with a space in the shell
LeetCode 241. 为运算表达式设计优先级
网络安全必会的基础知识
Drawing maze EasyX library with recursive backtracking method
Get the link behind? Parameter value after question mark
Concurrent programming (VI) ABA problems and solutions under CAS
URL backup 1
too many open files解决方案
[rust notes] 06 package and module
Too many open files solution
状态压缩DP AcWing 291. 蒙德里安的梦想
Find the combination number acwing 885 Find the combination number I