当前位置:网站首页>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;
}
}
边栏推荐
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
- Notes and bugs generated during the use of h:i:s and y-m-d
- Facial expression recognition based on pytorch convolution -- graduation project
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- Deep parsing JVM memory model
- Analysis of Alibaba canal principle
- 即时通讯IM,是时代进步的逆流?看看JNPF怎么说
- 请求参数的发送和接收
- Arbre DP acwing 285. Un bal sans patron.
- Methods of checking ports according to processes and checking processes according to ports
猜你喜欢
随机推荐
LeetCode 715. Range 模块
状态压缩DP AcWing 91. 最短Hamilton路径
JS ternary operator - learning notes (with cases)
Query XML documents with XPath
String splicing method in shell
[rust notes] 09- special types and generics
JS non Boolean operation - learning notes
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
[rust notes] 13 iterator (Part 1)
What is the difference between sudo apt install and sudo apt -get install?
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
Dom4j遍历和更新XML
剑指 Offer II 029. 排序的循环链表
Method of intercepting string in shell
Deep parsing JVM memory model
20220630学习打卡
LeetCode 513. 找树左下角的值
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
Find the combination number acwing 885 Find the combination number I