当前位置:网站首页>Sword finger offer II 029 Sorted circular linked list
Sword finger offer II 029 Sorted circular linked list
2022-06-25 12:59:00 【Small white yards fly up】
A word summary
Pay attention to various boundary problems , For example, an empty linked list , A linked list of elements , And a linked list with equal values
subject
A point in a monotone nondecreasing list of a given cycle , Write a function to insert a new element into the list insertVal , Make the list still cyclic ascending .
Given can be the pointer to any vertex in the list , Not necessarily a pointer to the smallest element in the list .
If there are multiple insertion positions that meet the conditions , You can choose any location to insert a new value , After insertion, the whole list remains in order .
If the list is empty ( The given node is null), You need to create a circular sequence table and return this node . otherwise . Please return to the original given node .
link :https://leetcode.cn/problems/4ueAj6
Ideas
Because it is monotonic and non decreasing , So you can traverse along the incoming header . Of course , Remember the previous node when traversing . When the given value size is between two nodes , Insert a new node .
After all kinds of debugging , I found the most difficult part of the problem , The point is that you have to consider a variety of special scenarios . For example, an empty linked list is given , A linked list with only one element is given , The given value is not between the maximum and minimum values of the linked list . It's exploding .
solution
Code
public Node insert(Node head, int insertVal) {
if (head == null) {
head = new Node(insertVal);
head.next = head;
return head;
}
if (head.next == head) {
Node node = new Node(insertVal);
head.next = node;
node.next = head;
return head;
}
Node current = head.next;
Node pre = head;
boolean isRound = false;
while (true) {
if ((pre.val <= insertVal && current.val >= insertVal) ||
(pre.val > current.val && (pre.val < insertVal || current.val > insertVal))) {
Node node = new Node(insertVal);
pre.next = node;
node.next = current;
break;
}
// I ran a lap without success , It means that the values of the linked list are the same , So just plug it in
if (pre == head) {
if (isRound) {
Node node = new Node(insertVal);
pre.next = node;
node.next = current;
break;
}
isRound = true;
}
current = current.next;
pre = pre.next;
}
return head;
}
边栏推荐
- 原生js---无限滚动
- 百度搜索稳定性问题分析的故事
- 剑指 Offer 04. 二维数组中的查找
- 英语口语 - 弱读
- Elemntui's select+tree implements the search function
- Parse JSON format data and save it to entity class
- Fedora 35 部署DNS主从和分离解析 —— 筑梦之路
- My first experience of go+ language -- a collection of notes on learning go+ design architecture
- JSTL tag: fmt:formatdate tag format Chinese standard time or timestamp
- Visual studio2019 link opencv
猜你喜欢
随机推荐
Spoken English - weak reading
Jenkins pipeline uses
Write regular isosceles triangle and inverse isosceles triangle with for loop in JS
[visio] solving the fuzzy problem of parallelogram in word
深圳民太安智能二面_秋招第一份offer
剑指 Offer II 032. 有效的变位词
[Visio]平行四边形在Word中模糊问题解决
My first experience of go+ language -- a collection of notes on learning go+ design architecture
Online service emergency research methodology
20220620 interview reply
剑指 Offer 04. 二维数组中的查找
3+1 guarantee: how is the stability of the highly available system refined?
MySQL 学习笔记
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
百度搜索稳定性问题分析的故事
Slice() and slice() methods of arrays in JS
英语口语 - 连读
A half search method for sequential tables
MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
Reload cuda/cudnn/pytorch