LeetCode #237 Medium

Delete Node in a Linked List

You are given only the node to delete (never the tail), with no access to the head. Delete it.

linked-list
Open on LeetCode ↗
02

Intuition

💡

You cannot reach the previous node to rewire it. So delete by impersonation: copy the next node's value into this node, then unlink the next node. From outside, the target value has vanished.

03

Approach

1

The usual deletion is impossible here

Normally you delete a node by finding its predecessor and rerouting prev.next past it. But this problem hands you only the node itself — no head, no way to walk to the predecessor. So the standard technique simply can't run, and we need a different mental model of what 'delete' means.

2

Delete by impersonation, not removal

If you can't remove this node, make it become the next node instead. Copy the successor's value into this node (node.val = node.next.val), then bypass the successor (node.next = node.next.next). After that, this node carries the value that used to be next, and the original next node is unreachable. From the outside, the value you were asked to delete has vanished and the list reads identically.

3

Two writes, and why the guarantee matters

It's literally two pointer assignments. The whole trick depends on node.next existing — which is exactly why the problem promises the given node is never the tail. If it were the tail there'd be no successor to copy from, and the technique would fail. O(1) time, O(1) space.

04

Solution & live demo

python
1class Solution:
2 def deleteNode(self, node):
3 node.val = node.next.val
4 node.next = node.next.next
05

Edge cases

Deleting a middle node

Its successor's value is copied in and the successor is skipped — the list reads identically minus the target value.

Guarantee it is never the tail

Relies on node.next existing; the problem promises the node is not last, so this is safe.

Duplicate values nearby

Only the given node is altered; other nodes with the same value are untouched.

06

Complexity

Time
O(1)
Space
O(1)
Two constant-time pointer writes.