• 欢迎光临~

# 代码随想录训练营第三天 ｜203.移除链表元素, 707.设计链表, 206.反转链表

203. 移除链表元素

```class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode temp = new ListNode(-1);
ListNode cur = temp;

while(cur.next!=null){
if(cur.next.val == val){
cur.next = cur.next.next;
}
else{
cur = cur.next;
}
}
return temp.next;
}
}```

707. 设计链表

```class ListNode{
int val;
ListNode next;
public ListNode(){};
public ListNode(int val){
this.val = val;

}
public ListNode(int val, ListNode next){
this.val = val;
this.next = next;
}

}
private int size;
size = 0;
}

public int get(int index) {
if(index < 0 || index >= size){
return -1;
}
for(int i = 0;i<= index; i++){
temp = temp.next;
}
return temp.val;
}

}

}

public void addAtIndex(int index, int val) {
if(index < 0 || index > size){
return;
}
size ++;
for(int i = 0; i<index; i++){
temp = temp.next;
}
ListNode cur = new ListNode(val);
cur.next = temp.next;
temp.next = cur;
}

public void deleteAtIndex(int index) {
if(index < 0 || index>= size){
return;
}
size--;
for(int i = 0;i < index; i++){
temp = temp.next;
}
temp.next = temp.next.next;
}
}```

206. 反转链表

```class Solution {
ListNode pre = null;
ListNode post = null;

while(cur != null){
post = cur.next;
cur.next = pre;
pre = cur;
cur = post;
}
return pre;
}
}```