# 203.移交链表元素

# 题目

删除链表中等于给定值val的所有节点

示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
1
2
3

# 解答

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function(head, val) {
    let root = new ListNode() // 傀儡节点
    root.next = head
    let curr = root
    while(curr) {
        let next = curr.next
        if(!next) {
            break;
        }
        if(next.val === val) {
            curr.next = curr.next.next
        } else {
            curr = curr.next
        }

    }
    return root.next
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30