To delete the node after a given node prev: prev.next = prev.next.next That's it. One line. The deleted node becomes unreachable and gets garbage collected.
But what if you need to delete a node and you only have a reference to that node itself, not its predecessor? You can't easily find the predecessor in a singly linked list.
The trick: copy the next node's value into the current node, then delete the next node. node.val = node.next.val node.next = node.next.next This doesn't work for the last node since there's no next node to copy from. Time: . Space: .