/*
 * @lc app=leetcode.cn id=203 lang=javascript
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {
  if (head === null) return null;
  const dummy = new ListNode(0, head);

  let cur = dummy;
  while (cur.next) {
    if (cur.next.val === val) {
      cur.next = cur.next.next;
      continue
    }
    cur = cur.next
  }
  return dummy.next;
};
// @lc code=end

function ListNode(val, next) {
  this.val = val || undefined
  this.next = next || null
}

const n1 = new ListNode(1)
const n2 = new ListNode(2)
const n3 = new ListNode(3)
const n4 = new ListNode(4)
const n5 = new ListNode(5)
const n6 = new ListNode(6)

n1.next = n2
n2.next = n6
n6.next = n3
n3.next = n4
n4.next = n5

console.log(removeElements(n1, 6))