Remove Linked List Elements
Description
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5
Solution(javascript)
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
// const removeElements = (head, val) => {
//   const aux = (current, acc) => {
//     if (!current) {
//       return acc
//     }
//     if (current.val === val) {
//       return aux(current.next, acc)
//     }
//     acc.next = {
//       val: current.val,
//       next: null,
//     }
//     return aux(current.next, acc.next)
//   }
//   const initial = { val: null, next: null }
//   aux(head, initial)
//     return initial.next
// }
const removeElements = (head, val) => {
  if (!head) {
    return head
  }
  if (head.val === val) {
    return removeElements(head.next, val)
  }
  head.next = removeElements(head.next, val)
  return head
}