当前位置:网站首页>剑指 Offer II 029. 排序的循环链表
剑指 Offer II 029. 排序的循环链表
2022-07-03 08:49:00 【Sasakihaise_】
【分析】因为插入后链表仍然有序,所以我们遍历链表,找到第一个比要插入的元素大的节点,把这个元素插在这个节点前面就可以了,加下来就是一些细节问题了。
1.首先因为要返回head,所以我们先弄个标记存一下head,令Node dummy = head,然后head当游标来用。
2.因为要插在一个节点前面,所以需要这个节点的前一个节点,因为题目中只给了head,所以我们没法知道head的pre,于是直接把head当pre,head的next来当head即可。
3.但是如果我们一开始的元素就已经比要插入的元素大很多了,比如5,7,0,2,插入1,我们发现我们定的head 7(原head5的next7)已经是大于1的了,但是1不应该插在7的前面。所以这种情况下我们需要找到链表最小的头,我们发现头0有一个特点就是他的pre要>head,但是呢也不排除3,3,3,3这种情况,所以还要判断一下是不是又回到了dummy。
4.链表为空和链表只有一个节点的情况需要额外注意一下子。
几个特殊样例:
[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] consistency hash
- 剑指 Offer II 091. 粉刷房子
- Format - C language project sub file
- Concurrent programming (III) detailed explanation of synchronized keyword
- [rust notes] 02 ownership
- 22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
- 求组合数 AcWing 886. 求组合数 II
- Alibaba canaladmin deployment and canal cluster Ha Construction
- LinkedList set
- Solution of 300ms delay of mobile phone
猜你喜欢
Format - C language project sub file
Sending and receiving of request parameters
Binary to decimal, decimal to binary
Vscode connect to remote server
Annotations simplify configuration and loading at startup
高斯消元 AcWing 883. 高斯消元解线性方程组
ES6 promise learning notes
20220630 learning clock in
Deep parsing (picture and text) JVM garbage collector (II)
[redis] redis persistent RDB vs AOF (source code)
随机推荐
[concurrent programming] concurrent security
【Rust笔记】06-包和模块
剑指 Offer II 091. 粉刷房子
The method of replacing the newline character '\n' of a file with a space in the shell
[concurrent programming] collaboration between threads
Monotonic stack -84 The largest rectangle in the histogram
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
22-05-26 西安 面试题(01)准备
LeetCode 241. 为运算表达式设计优先级
C language file reading and writing
Alibaba canaladmin deployment and canal cluster Ha Construction
【Rust 笔记】11-实用特型
LeetCode 871. 最低加油次数
Dom4j遍历和更新XML
单调栈-42. 接雨水
producer consumer problem
DOM 渲染系统(render mount patch)响应式系统
DOM render mount patch responsive system
too many open files解决方案
【Rust笔记】02-所有权