当前位置:网站首页>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;
}
}边栏推荐
- Binary to decimal, decimal to binary
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- How to use Jupiter notebook
- Markdown learning
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- Life cycle of Servlet
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- LeetCode 508. 出现次数最多的子树元素和
- [rust notes] 13 iterator (Part 1)
- The method for win10 system to enter the control panel is as follows:
猜你喜欢

低代码前景可期,JNPF灵活易用,用智能定义新型办公模式

AcWing 788. 逆序对的数量
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

Deeply understand the underlying data structure of MySQL index

LeetCode 532. 数组中的 k-diff 数对

LeetCode 715. Range 模块

状态压缩DP AcWing 91. 最短Hamilton路径

Gif remove blank frame frame number adjustment

Concurrent programming (III) detailed explanation of synchronized keyword

Gaussian elimination acwing 883 Gauss elimination for solving linear equations
随机推荐
网络安全必会的基础知识
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
拯救剧荒,程序员最爱看的高分美剧TOP10
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Redux - learning notes
Concurrent programming (III) detailed explanation of synchronized keyword
Allocation exception Servlet
DOM render mount patch responsive system
Methods of using arrays as function parameters in shell
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
Wonderful review | i/o extended 2022 activity dry goods sharing
状态压缩DP AcWing 91. 最短Hamilton路径
Format - C language project sub file
状态压缩DP AcWing 291. 蒙德里安的梦想
分配异常的servlet
求组合数 AcWing 885. 求组合数 I
createjs easeljs
第一个Servlet
AcWing 785. 快速排序(模板)
PHP uses foreach to get a value in a two-dimensional associative array (with instances)