# [203] 移除链表元素

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

示例:

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

输出: 1->2->3->4->5

注意头结点也是可能被删去的节点,因此需要补充建立一个虚拟的dummy节点,next指向head

var removeElements = function(head, val) {
  const dummy = {
    next: head
  };
  let cur = dummy;
  while (cur.next) {
    if (cur.next.val === val) {
      const newNext = cur.next.next || null;
      cur.next = newNext;
    } else {
      cur = cur.next;
    }
  }
  return dummy.next;
};

function ListNode(val) {
  this.val = val;
  this.next = null;
}