Delete Node in a Linked List
Delete a node from a singly linked list in O(1) time when only given direct access to the node itself and not the head.
01The problem
There is a singly-linked list head and we want to delete a node in it.
You are given the node to be deleted node. You will not be given access to the first node of head.
All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
- The value of the given node should not exist in the linked list.
- The number of nodes in the linked list should decrease by one.
- All the values before
nodeshould be in the same order. - All the values after
nodeshould be in the same order.
| Input | Output | Why |
|---|---|---|
[4, 5, 1, 9], node = 5 | [4, 1, 9] | Value 5 is removed; list becomes 4 -> 1 -> 9 |
[4, 5, 1, 9], node = 1 | [4, 5, 9] | Value 1 is removed; list becomes 4 -> 5 -> 9 |
[1, 2], node = 1 | [2] | First node removed; only 2 remains |
Constraints: 2 to 1,000 nodes, unique values, given node is guaranteed not to be the tail.
02How to spot it
- The problem specifies that you are not given the head, only the target node to delete.
- In a singly linked list, normal deletion requires updating
prev.next = node.next, which is impossible without the predecessor. - Key observation: You cannot rewire the incoming pointer to your node, but you can overwrite what your node contains.
- Copy the value of
node.nextintonode, then bypass and unlinknode.next.
So: value copy and link bypass trick.
03Pseudocode
node.val = node.next.val
node.next = node.next.next- Look at the next node
node.next. - Copy its value into current node:
node.val = node.next.val. - Unlink the next node:
node.next = node.next.next.
04Watch it work
linked list
Given reference to node with value 5 at index 1. No access to head or predecessor.
Pseudocode
- node.val = node.next.val
- node.next = node.next.next
Variables
- targetNode
- 5
05Step-by-step dry run
- 1
linked list
40511293nodeGiven reference to node with value 5 at index 1. No access to head or predecessor.
targetNode = 5
- 2
linked list (value copied)
40111293nodenextCopy next node's value into current node: node.val = 1. Current node now holds 1.
node.val = 1
- 3
linked list (node unlinked)
401192nodeBypass next node: node.next = node.next.next. The duplicate node is bypassed and unlinked.
result = 4,1,9
06Java solution
public class DeleteNodeInLinkedList {
static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public static void deleteNode(ListNode node) {
// Copy the next node's value into the current node
node.val = node.next.val;
// Bypass and unlink the next node
node.next = node.next.next;
}
public static void main(String[] args) {
ListNode head = new ListNode(4);
head.next = new ListNode(5);
head.next.next = new ListNode(1);
head.next.next.next = new ListNode(9);
// Delete the node containing value 5
deleteNode(head.next);
ListNode curr = head;
while (curr != null) {
System.out.print(curr.val + (curr.next != null ? " " : ""));
curr = curr.next;
}
System.out.println();
}
}4 1 9
Tricky lines, decoded
node.val = node.next.val: The node with value 5 adopts the value 1. The list temporarily reads4 -> 1 -> 1 -> 9.node.next = node.next.next: Unlinks the second 1, resulting in the clean sequence4 -> 1 -> 9.
07Why it works
In an object-oriented memory model, an external consumer only observes a linked list as a sequence of accessible values. Because we cannot alter the pointer pointing to node, we turn node into an exact clone of its successor, then delete the duplicate successor. The effect on all traversals is indistinguishable from true node deletion.
Invariant: After execution, the list retains all elements in original order except for the original node.val.
08Time and space
- Time: O(1). Exactly two reference and value assignments with no list traversal.
- Space: O(1). Modifies the structure in place without extra memory.
09Edge cases
| Input | Output | Why it matters |
|---|---|---|
[4, 5, 1, 9], node = 5 | [4, 1, 9] | Middle node deletion |
[1, 2], node = 1 | [2] | Deleting the initial node without head |
[0, 1], node = 0 | [1] | Node with value zero |
[-3, 5, -99], node = -3 | [5, -99] | Negative values |
[1, 2, 3, 4], node = 3 | [1, 2, 4] | Second-to-last node |
10Common mistakes
- Writing
node = null. This only reassigns the local parameter reference. The original node in the heap remains connected to the list. - Attempting to traverse backwards. Singly linked lists have no backward pointers; without
head, reaching the predecessor is impossible. - Applying this technique to the tail node. If
node.next == null,node.next.valthrows aNullPointerException. The problem guaranteesnodeis never the tail.
11Other approaches
| Approach | Time | Space | Use when |
|---|---|---|---|
| Standard head deletion | O(n) | O(1) | When head reference is available |
| Copy + skip | O(1) | O(1) | Default solution when head is missing |
| Shift all values left | O(n) | O(1) | Unnecessary; copy once is sufficient |
12In the interview
- What if the target node were the tail? Without access to
heador double-linking, deleting a tail node inO(1)is mathematically impossible because the preceding node must have itsnextpointer cleared tonull.
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. Why does node = null fail to delete the node from the linked list?
2. Why cannot this algorithm delete the tail node of a singly linked list?
14Solve it yourself
Try implementing standard deletion when head is provided:
- Walk a
currpointer fromheadwhile checkingcurr.next.val == target. - Handle the edge case where
head.val == targetusing a dummy node.