Algorithm for deleting one element in an single linked list with O1 complexity
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Deleting an element from a singly linked list is generally an O(n) operation because finding the node before the one you wish to delete usually requires traversing the list from the head. However, there is a special case where you can delete an element in O(1) time if you are given a pointer to the node you wish to delete. This can be particularly useful in scenarios where you have direct access to the node itself, rather than just the key to search for the node.
Technical Explanation
Assumptions
Before we delve into the mechanism for achieving O(1) deletion, it is essential to note the following assumptions:
- You are given direct access to the node
node_to_deletethat you want to remove from the linked list. - The node
node_to_deleteis not the last node in the linked list. Deleting the last node in O(1) time is not feasible without additional information like a pointer to the previous node.
The O(1) Deletion Mechanism
Given these conditions, you can simply perform the following steps to delete the node node_to_delete
:
- Copy the data from the node after
node_to_deleteintonode_to_delete. - Update the link of
node_to_deleteto point to the node after its next node. - Effectively, the
nextnode is removed from the list, and its data is replaced by the data from the subsequent node.
Here is a visualization and the corresponding code for this operation:
Visualization
- Suppose you have the nodes:
[A] -> [B] -> [C] -> [D]. - You have been given direct access to node
[B]. - The operation proceeds as follows:
- Copy data from
[C]to[B]. - Change the
nextof[B]from[C]to[D].
Resulting list: [A] -> [C] -> [D]
.
Code Example
Here's how this can be implemented in C:
- Not Applicable for Last Node: This approach cannot delete the last node of a singly linked list, as it relies on the next node existing.
- Memory Handling: It requires proper memory handling for the removed node to avoid memory leaks when applicable.
- Real-time Systems: Where constant time operations are required to meet strict time constraints.
- Certain Applications: Where access patterns to nodes may allow only point-based access rather than sequential traversal.
- Doubly Linked Lists: For scenarios where the last node might need to be deleted with O(1) complexity, a doubly linked list can be considered, which requires keeping an additional
prevpointer. - Head Pointer as Argument: If you're okay with O(n) complexity but want a safer function that can handle deletion of any node, including the last one, modifying the function to take the head pointer as an argument might be an alternative.
Related reading
- Algorithm for dependency resolution
- Algorithm for detecting clusters of dots
- Algorithm for detecting duplicates in a dataset which is too large to be completely loaded into memory
- Algorithm for detecting full loop when iterating over a list
- Algorithm for diameter of graph?
- Algorithm for downsampling array of intervals
- Algorithm for Determining Tic Tac Toe Game Over
- Algorithm for determining whether a point is inside a 3D mesh

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.