---
# System prepended metadata

title: \#203 Remove Linked List Elements
tags: [leetcode, leetcode-easy]

---

# \#203 Remove Linked List Elements
## *實作linked list刪除符合指定元素值的node*
## Log
 - build 20210425 by syhuang

## 遞回解法
```javascript=
var removeElements = function(head, val) {
    if(!head) return null;
    head.next = removeElements(head.next, val);
    return head.val==val?head.next:head;
};
```

## 參考答案
 - pointer p從head開始跑迴圈逐個檢查, 如果p.next.val是要刪除的, 就把p.next覆蓋為p.next.next
 - edge case為head.val是要刪除的元素, 那麼就回傳head.next即可; 或是head本身就是null
```javascript=
var removeElements = function(head, val) {
    if(!head) return null;
    var p = head;
    while(p.next){
        if(p.next.val == val) p.next = p.next.next;
        else p = p.next;
    }
    return head.val==val?head.next:head;
};
```
## 備註
## 參考
 - [參考解答](https://leetcode.com/problems/remove-linked-list-elements/discuss/275445/Javascript-simple-solution)
###### tags: `leetcode`, `leetcode-easy`