How to delete a node in the middle of a singly linked list

Here is one way to delete a target node in the middle of a singly linked list:

deleteMiddleNodeTry in REPL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function deleteMiddleNode(node) {
if (!node || !node.next) return;
const data = node.next.data;
const newNext = node.next.next;
node.data = data;
node.next = newNext;
}
function Node(data) {
this.next = null;
this.data = data;
}
Node.prototype.addToTail = function(data) {
const end = new Node(data);
let current = this;
while (current.next !== null) {
current = current.next;
}
current.next = end;
return end;
};

Tests can be found at REPL.it