当前位置:网站首页>2.2 链表---设计链表(Leetcode 707)
2.2 链表---设计链表(Leetcode 707)
2022-06-12 07:58:00 【Amoni_】
class MyLinkedList {
public:
// 定义链表节点结构体
struct ListNode {
int val;
ListNode* next;
ListNode(): val(0), next(nullptr) {
}
ListNode(int x): val(x), next(nullptr) {
}
ListNode(int x, ListNode* next): val(x), next(next) {
}
};
// 构造函数
MyLinkedList() {
_dummyHead = new ListNode(0);// 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点
_size = 0;
}
// 获取到第index个节点数值,如果index是非法数值直接返回-1, 注意index是从0开始的,第0个节点就是头结点
int get(int index) {
if(index > (_size-1) || index < 0){
// 非法数值(_size计算了dummyHead,因此需要-1)
return -1;
}
ListNode* cur = _dummyHead->next; // 初始化为头结点
while(index--){
cur = cur->next;
}
return cur->val;
}
// 在链表最前面插入一个节点,插入完成后,新插入的节点为链表的新的头结点
void addAtHead(int val) {
ListNode* newNode = new ListNode(val);
newNode->next = _dummyHead->next;
_dummyHead->next = newNode;
_size++;
}
// 在链表最后面添加一个节点
void addAtTail(int val) {
ListNode* newNode = new ListNode(val);
ListNode* cur = _dummyHead; // 注意这里是_dummyHead
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = newNode;
_size++;
}
// 在链表中的第 index 个节点之前添加值为 val 的节点。
// 1. 如果 index 等于链表的长度,则该节点将附加到链表的末尾。
// 2. 如果 index 大于链表长度,则不会插入节点。
// 3. 如果 index 小于0,则在头部插入节点。
void addAtIndex(int index, int val) {
if(index > _size){
return ;
}
ListNode* newNode = new ListNode(val);
ListNode* cur = _dummyHead; // 注意这里是_dummyHead
while(index--){
cur = cur->next;
}
newNode->next = cur->next; // 注意连接顺序
cur->next = newNode;
_size++;
}
// 如果索引 index 有效,则删除链表中的第 index 个节点
void deleteAtIndex(int index) {
if(index >= _size || index < 0){
return ;
}
ListNode* cur = _dummyHead; // 注意这里是_dummyHead
while(index--){
cur = cur->next;
}
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
_size--;
}
private:
int _size ;
ListNode* _dummyHead;
};
/** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList* obj = new MyLinkedList(); * int param_1 = obj->get(index); * obj->addAtHead(val); * obj->addAtTail(val); * obj->addAtIndex(index,val); * obj->deleteAtIndex(index); */
边栏推荐
- Process terminated
- Improvement of hash function based on life game (continued 1)
- Search and rescue strategy of underwater robot (FISH)
- Topic 1 Single_Cell_analysis(4)
- R language uses rstudio to save visualization results as PDF files (export--save as PDF)
- 20220607. 人脸识别
- MFC中窗口刷新函数详解
- Kalman filter encapsulation function
- R language dplyr package mutate_ At function and one_ The of function converts the data type of a specified data column (specified by a vector) in dataframe data to a factor type
- The R language uses the sample The split function divides the machine learning data set into training set and test set
猜你喜欢

Dynamic simulation method of security class using Matlab based Matpower toolbox

Fundamentals of Mathematics - Taylor Theorem

Literature reading: raise a child in large language model: rewards effective and generalizable fine tuning

Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano

Topic 1 Single_Cell_analysis(4)

Pytorch installation (GPU) in Anaconda (step on pit + fill pit)

Database connection pool and dbutils tool
![[redistemplate method details]](/img/ef/66d8e3fe998d9a788170016495cb10.png)
[redistemplate method details]

从AC5到AC6转型之路(1)——补救和准备

Symfony 2: multiple and dynamic database connections
随机推荐
The project file contains toolsversion= "14.0". This toolset may be unknown or missing workarounds
How to standardize the creation of a pytorch project
OpenMP task 原理與實例
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
Chapter 3 - Fundamentals of cryptography
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water
Topic 1 Single_ Cell_ analysis(1)
Mathematical knowledge - derivation - Basic derivation knowledge
20220607. face recognition
MinGW offline installation package (free, fool)
2021.10.26 scientific research log
20220524 backbone深度学习网络骨架
Mathematical Essays: Notes on the angle between vectors in high dimensional space
Leetcode notes: Weekly contest 295
Kalman filter encapsulation function
Work summary of the week from November 22 to 28, 2021
Topic 1 Single_Cell_analysis(2)
数值计算方法 Chapter6. 解线性方程组的迭代法
Procedure execution failed 1449 exception
Compiling principle on computer -- functional drawing language (I)