How to remove all nodes with a value k from singly linked list

Here is one means of removing all nodes with a value k from a singly linked list:

removeKFromListTry in REPL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Definition for singly-linked list:
// function ListNode(x) {
// this.value = x;
// this.next = null;
// }
//
// Try to solve this task in O(n) time using O(1) additional space, where n is the number of elements in the list
// Recursive solution, uses O(n) space because of stack, no tail call optimization
function removeKFromList(l, k) {
if (!l) {
return null;
} else {
l.next = removeKFromList(l.next, k);
return l.value === k ? l.next : l;
}
}

Tests can be found at REPL.it