Middle of the Linked List
Problem Statement:β
Given theΒ headΒ of a singly linked list, returnΒ the middle node of the linked list.
If there are two middle nodes, returnΒ the second middleΒ node.
Example 1:

Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.
-
Example:
β Solution: Two Pointers (Tortoise-Hare)β
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode *slow = head;
ListNode *fast = head;
// Move fast by 2 and slow by 1 step
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
// When fast reaches end, slow is at middle
return slow;
}
};
π How It Worksβ
- We use two pointers:
slowmoves one node at a time.fastmoves two nodes at a time.
- When
fastreaches the end,slowwill be at the middle node. - In case of even-length list, this returns the second middle node.
This is also called the Tortoise-Hare algorithm.
π§© Key Formula / Transitionβ
slow = slow->nextfast = fast->next->next
This ensures slow reaches halfway when fast finishes the list.
β±οΈ Time & Space Complexityβ
| Metric | Value |
|---|---|
| Time | O(N) |
| Space | O(1) |
Only one traversal, and no extra space.
β οΈ Edge Casesβ
- List is empty β returns
NULL - List has one node β returns that node
- List has even number of nodes β returns second middle (as required by LeetCode)
π‘ Other Approachesβ
| Approach | Time | Space | Notes |
|---|---|---|---|
| Count & Traverse | O(N) | O(1) | First pass to count nodes, second to reach middle |
| Store in vector | O(N) | O(N) | Simplest but extra space |
π Related Problemsβ
- Detect Cycle in Linked List (uses same fast/slow logic)
- Find the N-th node from end
- Reverse a Linked List
- Palindrome Linked List (use middle to compare halves)
π Level Up Your Engineering Mindset
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Official Hub
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.