# Leetcode链表（basic(1)）

（下面附上完整cpp代码大佬勿喷

## 单链表的增删改查

#include <iostream>
using namespace std;

class ListNode{
public:
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){};
};
public:
/** Initialize your data structure here. */
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
return -1;

int i = 0;
while(p!=NULL){
if(i==index){
return p->val;
}
p = p->next;
++i;
}
return -1;
}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
ListNode *q = new ListNode(val);
}

/** Append a node of value val to the last element of the linked list. */
while(p->next!=NULL){
p = p->next;
}
ListNode *q = new ListNode(val);
q->next = p->next;
p->next = q;
}
void print(){
while(p!=NULL){
cout<<p->val<<" ";
p = p->next;
}
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val) {
if(index<=0){
return;
}
int i = 0;
while(p->next!=NULL){
if(i==index){
ListNode *node = new ListNode(val);
node->next = p->next;
p->next = node;
}
p = p->next;
++i;
}
//如果是最后一个节点
if(i==index){
ListNode *node = new ListNode(val);
node->next = p->next;
p->next = node;
}
}

/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
int i = 0;
while(p->next!=NULL){
if(i==index){
p->next = p->next->next;
return;
}
p = p->next;
++i;
}
}
};

/**
* int param_1 = obj->get(index);
* obj->deleteAtIndex(index);
*/
int main(void){
int index = 1;
int val = 10;
int param_1 = obj->get(index);
cout<<param_1<<endl;
obj->print();
cout<<endl;
obj->deleteAtIndex(2);
obj->print();
return 0;
}


## 双链表的增删改查

#include <iostream>
using namespace std;

struct ListNode
{
int val;
ListNode *pre;
ListNode *next;
ListNode(int x):val(x),pre(NULL),next(NULL){};
};

public:
//设置头尾两个哨兵节点
ListNode *tail;
int size = 0;
/** Initialize your data structure here. */
tail = nullptr;
size = 0;
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
return -1;

int i = 0;
while(p!=NULL){
if(i==index){
return p->val;
}
p = p->next;
++i;
}
return -1;
}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
ListNode *q = new ListNode(val);
}else{
}
size++;

}

/** Append a node of value val to the last element of the linked list. */
if(tail!=nullptr){
ListNode *q = new ListNode(val);
tail->next = q;
q->pre = tail;
tail = q;
}else{
//如果尾节点为空，则头结点也为空
tail = new ListNode(val);
}
size++;

}

/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val) {
if(index<=0){
return;
}
//如果是最后一个节点
if(size==index){
return;
}
if(index>size)
return;
int i = 0;
while(p->next!=NULL){
if(i==index){
ListNode *node = new ListNode(val);
node->next = p;
node->pre = p->pre;
p->pre->next = node;
p->pre = node;
size++;
break;
}
p = p->next;
++i;
}
}

/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
//链表为空，不能删除
return;
int i = 0;
if(index==0){
if(p->next==nullptr){
tail = nullptr;//将尾部置空

}else{
}
delete p;
size--;
return;
}
if(index==size-1){
p = tail;
tail = tail->pre;
if(tail){
tail->next = nullptr;
}
delete p;
size--;
return;
}
while(p->next!=NULL){
if(i==index){
p->pre->next = p->next;
p->next->pre = p->pre;
delete p;
size--;
return;
}
p = p->next;
++i;
}

}
};
/**
* int param_1 = obj->get(index);
* obj->deleteAtIndex(index);
*/
int main(void){
for(int i = 0; i<obj.size;++i){
cout<<obj.get(i)<<" ";
}
cout<<endl;
obj.deleteAtIndex(2);
for(int i = 0; i<obj.size;++i){
cout<<obj.get(i)<<" ";
}
return 0;
}


07-13 613
09-17 34