Algorithm

Delete Node

Linked List Pattern

Delete Node in a Linked List

You are given a reference to one node inside a singly linked list — the node to delete — and nothing else: no head, no way to reach the list from the front. Remove that node so the list's values read exactly as if it were gone, in place. Nothing is returned. It is guaranteed the node is not the tail, so a successor always exists.

CONSTRAINTS
  • The number of nodes in the given list is in the range [2, 1000]
  • -1000 <= Node.val <= 1000
  • The node to be deleted is in the list and is not a tail node
EXAMPLE 1
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
The value 5 must disappear from the sequence. Afterward the list reads 4, 1, 9 — the remaining values in their original order.
EXAMPLE 2
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Removing the value 1 leaves 4, 5, 9. The relative order of every other value is unchanged.
Why am I not given the head of the list?
That's the whole challenge — without the head (and with no backward pointers) you cannot reach the target's predecessor, so the ordinary 'rewire the previous node' deletion is off the table. The input is restricted on purpose.
What if the node to delete were the tail?
Then it has no successor to copy from, and this technique cannot work. Deleting a tail with no predecessor reference is genuinely impossible in a singly linked list, which is why the problem guarantees the node is not the tail.
Does the exact node object I was given have to be the one removed?
No — the problem only requires the sequence of values to look right. It's fine (and necessary here) that the object you were handed survives while its successor is the node actually unlinked.
Could there be duplicate values that make this ambiguous?
You're given a direct reference to a specific node, not a value to search for, so there's no ambiguity about which node to remove even if several share a value.
Identity Theft

Deleting a node usually requires its "predecessor" (the node before it) to rewire the chain. But what if you only have a reference to the target node itself? In a singly linked list, you cannot look backward.

Since we can't delete ourselves by telling our predecessor to skip us, we do something clever: we steal the identity of our neighbor.

We copy the value and the pointer from the node after us into our own node. Effectively, we become our successor, and the original successor becomes redundant and is skipped.

python
# 1. Steal the value of the next node
node.val = node.next.val

# 2. Skip the next node by taking its pointer
node.next = node.next.next
The Constraint: Not for Tails

This "trick" only works if there is a next node to steal from. If the target is the Tail, this approach is impossible without a reference to the head.

Worked Example:[4, 5, 1, 9], delete 5
4
5
node
1
9
NULL
We start with a reference directly to the node containing 5, which we need to delete. We cannot access the preceding node containing 4.
4
1
node
1
9
NULL
We copy the value of the succeeding node (1) into our current node's value slot. Our node has now stolen its neighbor's identity.
4
1
node
1
9
NULL
We update our current node's next pointer to point directly to 9, effectively skipping and removing the duplicate neighbor node containing 1.
Interactive Strategy Visualization

O(1) Identity Theft

Technique: Pointer Reassignment
4target519
OperationIdentify target node (5)
Complexity
O(1) Time / O(1) Space
1. Identify Target
We are given node `5`. We don't have head access, so we can't search for its predecessor.
O(N) Predecessor Rewire (impossible here)
O(1) Copy-and-Splice Successor