当前位置:网站首页>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;
}
}
边栏推荐
- [MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
- Apache startup failed phpstudy Apache startup failed
- LeetCode 715. Range 模块
- SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
- Gif remove blank frame frame number adjustment
- ES6 promise learning notes
- 剑指 Offer II 029. 排序的循环链表
- LeetCode 30. 串联所有单词的子串
- Slice and index of array with data type
- 高斯消元 AcWing 883. 高斯消元解线性方程组
猜你喜欢
求组合数 AcWing 885. 求组合数 I
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
AcWing 788. 逆序对的数量
Divide candy (circular queue)
剑指 Offer II 029. 排序的循环链表
Character pyramid
Concurrent programming (III) detailed explanation of synchronized keyword
网络安全必会的基础知识
How to use Jupiter notebook
too many open files解决方案
随机推荐
Deep parsing JVM memory model
Markdown learning
ES6 promise learning notes
常见渗透测试靶场
精彩回顾|I/O Extended 2022 活动干货分享
Complex character + number pyramid
How to use Jupiter notebook
[concurrent programming] collaboration between threads
LeetCode 57. 插入区间
Convert video to GIF
XPath实现XML文档的查询
22-05-26 Xi'an interview question (01) preparation
Dom4j遍历和更新XML
LeetCode 1089. 复写零
[concurrent programming] explicit lock and AQS
记忆化搜索 AcWing 901. 滑雪
[concurrent programming] concurrent tool class of thread
[rust notes] 09- special types and generics
[rust notes] 02 ownership
剑指 Offer II 091. 粉刷房子