当前位置:网站首页>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笔记:Biweekly Contest 79
- Servlet advanced
- L'effet de l'oie sauvage sur l'économie numérique verte de Guilin
- 数值计算方法 Chapter6. 解线性方程组的迭代法
- 20220524 深度学习技术点
- Leetcode notes: Weekly contest 296
- Voice assistant -- Architecture and design of Instruction Assistant
- Topic 1 Single_Cell_analysis(3)
- Web page performance optimization interview questions
- Voice assistant -- Qu -- semantic role annotation and its application
猜你喜欢

Leverage contextual information

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

Chapter 4 - key management and distribution

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

Chapter 3 - Fundamentals of cryptography

Mathematical knowledge - derivation - Basic derivation knowledge

Transformation from AC5 to AC6 (1) - remedy and preparation

Chapter V - message authentication and digital signature

Dynamic simulation method of security class using Matlab based Matpower toolbox

Fundamentals of Mathematics - Taylor Theorem
随机推荐
Chapter 6 - identity authentication, Chapter 7 - access control
Compiling principle on computer -- function drawing language (II): lexical analyzer
Preliminary knowledge next New concepts such as isr/rsc/edge runtime/streaming in JS
Search and rescue strategy of underwater robot (FISH)
Fundamentals of Mathematics - Taylor Theorem
Classic paper review: palette based photo retrieval
Multithread decompression of tar
El expression and JSTL
Leetcode notes: Weekly contest 296
『Three.js』辅助坐标轴
Compiling principle on computer -- functional drawing language (IV): semantic analyzer
Literature reading: deep neural networks for YouTube recommendations
Improvement of hash function based on life game (continued 2)
C # push box
WEB页面性能优化面试题
Solve the problem of uploading sftporg apache. commons. net. MalformedServerReplyException: Could not parse respon
"Three.js" auxiliary coordinate axis
Voice assistant -- Qu -- semantic role annotation and its application
Principes et exemples de tâches OpenMP
Compiling principle on computer -- functional drawing language (I)