当前位置:网站首页>Linked list: orderly circular linked list
Linked list: orderly circular linked list
2022-06-13 02:45:00 【Zeng Qiang】
subject
https://leetcode-cn.com/problems/4ueAj6/
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 .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/4ueAj6
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking
The topic requires inserting nodes into the ordered circular linked list , And the new linked list still conforms to the ordered nature .
Then we need to traverse the circular linked list , Find two nodes , The front node is less than or equal to the node to be inserted , The rear node is greater than or equal to the node to be inserted .
Except for the insertion rules , Various boundary conditions also need to be considered .
- In the case of only one node
- Empty node
- Two nodes matching the insertion rule could not be found , Insert the new node after the largest node .
The specific code is as follows :
Code
/* // 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) {
Node node = new Node(insertVal);
if(head == null) {
head = node;
head.next = head;
} else if(head.next == head) {
head.next = node;
node.next = head;
} else {
insertCore(head, node);
}
return head;
}
private void insertCore(Node head, Node node) {
Node cur = head;
Node next = head.next;
Node biggest = head;
// Record the maximum
while(!(cur.val <= node.val && node.val <= next.val) && next != head) {
cur = next;
next = next.next;
if(biggest.val <= cur.val) {
biggest = cur;
}
}
if(cur.val <= node.val && node.val <= next.val) {
// Insert node
cur.next = node;
node.next= next;
} else {
node.next = biggest.next;
biggest.next = node;
}
}
}
summary
This topic examines the nature of circular linked list , You need to pay attention to how to calculate the maximum value of the linked list and how to modify the point of the linked list . Pay attention to the boundary conditions .
边栏推荐
- Rounding in JS
- Superficial understanding of conditional random fields
- Opencv 07, pixel read, change and bitmap write
- JS multiple judgment writing
- [reading point paper] deeplobv3+ encoder decoder with Atlas separable revolution
- Linear, integer, nonlinear, dynamic programming
- [reading papers] dcgan, the combination of generating countermeasure network and deep convolution
- [data analysis and visualization] key points of data drawing 11- precautions for radar chart
- [data analysis and visualization] key points of data mapping 7- over mapping
- Opencv 17 face recognition
猜你喜欢

Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)

在IDEA使用C3P0連接池連接SQL數據庫後卻不能顯示數據庫內容

02 优化微信开发者工具默认的结构

Introduction and download of common data sets for in-depth learning (with network disk link)
![[reading papers] visual convolution zfnet](/img/01/4181f19b2d24b842488522c2001970.jpg)
[reading papers] visual convolution zfnet

Matlab: obtain the figure edge contour and divide the figure n equally

How to destroy a fragment- How to destroy Fragment?

01 initial knowledge of wechat applet

Special topic I of mathematical physics of the sprint strong foundation program

How did you spend your winter vacation perfectly?
随机推荐
wx.createSelectorQuery()在components获取Dom节点的使用
Perfect square
For loop instead of while loop - for loop instead of while loop
OpenCVSharpSample05Wpf
[reading point paper] deeplobv3+ encoder decoder with Atlas separable revolution
[life science] DNA extraction of basic biological experiments
js 解构赋值
vant实现移动端的适配
02 optimize the default structure of wechat developer tools
AAR packaging and confusion
A wechat app for shopping
在IDEA使用C3P0连接池连接SQL数据库后却不能显示数据库内容
小程序 input,textarea组件权重比fixed的z-index都高
03 认识第一个view组件
Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
Implementing fillet in custom dialog
nn. Conv2d and nn Convtranspose2d differences
redis 多个服务器共用一个
Vant realizes the adaptation of mobile terminal
[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge