当前位置:网站首页>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); */
边栏推荐
- LeetCode笔记:Weekly Contest 295
- Detailed explanation of window refresh function in MFC
- Pytorch profiler with tensorboard.
- Servlet
- 2D visualization of oil storage, transportation and production, configuration application enables intelligent development of industry
- Numerical calculation method chapter6 Iterative method for solving linear equations
- Leetcode notes: Weekly contest 295
- Leetcode notes: Weekly contest 276
- Topic 1 Single_ Cell_ analysis(2)
- Leetcode notes: biweekly contest 71
猜你喜欢

Logistic regression

Mathematical knowledge - derivation - Basic derivation knowledge

The latest hbuilderx editing uni app project runs in the night God simulator

vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)

Topic 1 Single_ Cell_ analysis(4)

Vscode 1.68 changes and concerns (sorting and importing statements / experimental new command center, etc.)

tar之多线程解压缩

"Three.js" auxiliary coordinate axis

Scoring prediction problem

20220526 yolov1-v5
随机推荐
[RedisTemplate方法详解]
Topic 1 Single_ Cell_ analysis(2)
LeetCode笔记:Weekly Contest 295
The R language catools package divides the data, the scale function scales the data, the KNN function of the class package constructs a k-nearest neighbor classifier, and compares the model accuracy u
Voice assistant - potential skills and uncalled call technique mining
Data visualization and Matplotlib
经典论文回顾:Palette-based Photo Recoloring
The project file contains toolsversion= "14.0". This toolset may be unknown or missing workarounds
Topic 1 Single_ Cell_ analysis(1)
Clarify the division of IPv4 addresses
Search and rescue strategy of underwater robot (FISH)
Utilize user tag data
2、 Eight, ten and hexadecimal conversion
Chapter 2 - cyber threats and attacks
Voice assistant -- Architecture and design of Instruction Assistant
2021.11.3-7 scientific research log
Process terminated
HDLC protocol
Preliminary knowledge next New concepts such as isr/rsc/edge runtime/streaming in JS
The R language uses the sample The split function divides the machine learning data set into training set and test set