🔗 Try This Problem
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
🎥 Video Explanation
Problem Explanation
You are given the head of a singly linked list. The task is to remove the middle node and return the updated list.
The middle node is defined using 0-based indexing:
Where n is the total number of nodes.
Example
- n = 7
- Middle index = 3
- Node to remove = 7
Approach 1: Brute Force (Two Traversals)
Idea
- Traverse the list to count total nodes
- Compute middle index
- Traverse again to delete that node
Complexity
Limitation
This approach requires two passes, which is not optimal when a single traversal solution exists.
Approach 2: Fast and Slow Pointer (Optimal)
Intuition
Use two pointers:
- Slow pointer moves one step at a time
- Fast pointer moves two steps at a time
When the fast pointer reaches the end of the list:
Important Detail
To remove the middle node, access to the previous node is required.
Therefore, maintain an additional pointer:
Algorithm Steps
- Initialize:
- slow = head
- fast = head
- prev = null
- Traverse:
- Move fast by 2 steps
- Move slow by 1 step
- Update prev = slow (before moving slow)
- When loop ends:
- slow points to middle node
- prev points to node before middle
- Delete node:
Detailed Dry Run
Input
Initial State
Iteration 1
Iteration 2
Iteration 3
After Loop
Deletion
Result:
Optimized Code (Java)
Complexity Analysis
- Only one traversal of the linked list
- No extra data structures used
Edge Cases
Single Node
Two Nodes
Even Length List
- n = 4
- Middle index = 2
- Node removed = 3
Key Takeaways
- Fast and slow pointer reduces two-pass solution to one-pass
- Tracking the previous node is necessary for deletion
- Works efficiently for large linked lists
- A fundamental pattern used in multiple linked list problems
Conclusion
The fast and slow pointer technique provides an elegant and efficient way to identify and remove the middle node in a linked list. By leveraging different traversal speeds, the problem can be solved in a single pass with constant space, making it optimal for both interviews and practical implementations.




