当前位置:网站首页>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); */
边栏推荐
- Explanation and explanation on the situation that the volume GPU util (GPU utilization) is very low and the memory ueage (memory occupation) is very high during the training of pytoch
- Chapter 4 - key management and distribution
- 解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon
- Leetcode notes: biweekly contest 69
- Interview questions on mobile terminal, Android and IOS compatibility
- Database connection pool and dbutils tool
- Leetcode notes: Weekly contest 278
- 经典论文回顾:Palette-based Photo Recoloring
- 2021.11.2 scientific research log
- Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano
猜你喜欢

PPP agreement

Support vector machine (SVM)

Explain the basic working principle of Ethernet

Topic 1 Single_ Cell_ analysis(1)

System service configuration service - detailed version

最新hbuilderX编辑uni-app项目运行于夜神模拟器

Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water

Primal problem and dual problem

2、 Eight, ten and hexadecimal conversion

Architecture and performance analysis of convolutional neural network
随机推荐
Cookies and sessions
数值计算方法 Chapter6. 解线性方程组的迭代法
C # push box
Introduction to coco dataset
What is a good recommendation system?
Leetcode notes: Weekly contest 275
Some summaries of mathematical modeling competition in 2022
Dynamic simulation method of security class using Matlab based Matpower toolbox
In depth learning, the parameter quantity (param) in the network is calculated. The appendix contains links to floating point computations (flops).
The pit of FANUC machine tool networking
Process terminated
2021.10.26 scientific research log
Chapter V - message authentication and digital signature
The latest hbuilderx editing uni app project runs in the night God simulator
Search and rescue strategy of underwater robot (FISH)
经典论文回顾:Palette-based Photo Recoloring
Web page performance optimization interview questions
电脑连接上WiFi但是上不了网
Leetcode notes: biweekly contest 70
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water