# 60天刷题记录Day3|链表

42 阅读2分钟

### Leetcode 203. Remove Linked List Elements

``````class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dum = new ListNode(0);
auto p = dum;
while(p->next != NULL){
if(p->next->val == val){
p->next = p->next->next;
}else{
p = p->next;
}
}
return dum->next;
}
};
``````

### Leetcode 707. Design Linked List

``````class MyLinkedList {
public:
struct Node {
int val;
Node* next;
Node(int _val): val(_val), next(NULL) {};
}

int get(int index) {
if(index < 0) return -1;
for(int i = 0; i < index && p; i ++) p = p->next;
if(!p) return -1;
return p->val;
}

auto cur = new Node(val);
}

else{
while(p->next) p = p->next;
p->next = new Node(val);
}
}

void addAtIndex(int index, int val) {
else{
int len = 0;
for(auto p = head; p; p = p->next) len ++;
else if(index < len){
for(int i = 0; i < index - 1; i ++) p = p->next;
auto cur = new Node(val);
cur->next = p->next;
p->next = cur;
}
}
}

void deleteAtIndex(int index) {
int len = 0;
for(auto p = head; p; p = p->next) len ++;
if(index >= 0 && index < len){
else{
for(int i = 0; i < index - 1; i ++) p = p->next;
p->next = p->next->next;
}
}
}
};
``````

### Leetcode 206. Reverse Linked List

``````class Solution {
public: