Introduction

The Find the Middle of a Linked List problem is a very common DSA interview question related to linked lists. Interviewers like this problem because it checks your understanding of pointer movement, traversal logic, and optimization.

In simple words, this problem asks you to find the middle node of a linked list in one traversal.

What Does “Middle of a Linked List” Mean?

Given a linked list:

Example

Odd length list:

1 → 2 → 3 → 4 → 5

Middle node = 3

Even length list:

1 → 2 → 3 → 4 → 5 → 6

Middle node = 4 (second middle)

Why Brute Force Approach is Not Ideal

A simple approach is:

Problems with This Approach

Interviewers usually expect a single-pass solution.

Optimized Approach: Slow and Fast Pointer Technique

The most efficient way to solve this problem is by using two pointers.

Key Idea (Very Simple)

When the fast pointer reaches the end, the slow pointer will be at the middle.

Why This Technique Works

Because the fast pointer moves twice as fast:

So slow naturally lands on the middle node.

Step-by-Step Explanation

Steps:

  1. Initialize both slow and fast pointers at the head

  2. Move slow by one step

  3. Move fast by two steps

  4. Repeat until fast reaches the end or becomes null

  5. slow now points to the middle node

Dry Run Example (Odd Length)

Linked List:

1 → 2 → 3 → 4 → 5
StepSlowFast
Start11
123
235

Fast reaches end → Slow is at 3 (middle)

Dry Run Example (Even Length)

Linked List:

1 → 2 → 3 → 4 → 5 → 6
StepSlowFast
Start11
123
235
34null

Slow is at 4 (second middle)

Code Implementation

C++ Code

ListNode* middleNode(ListNode* head) {
    ListNode* slow = head;
    ListNode* fast = head;

    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

Java Code

public ListNode middleNode(ListNode head) {
    ListNode slow = head;
    ListNode fast = head;

    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}

Python Code

def middleNode(head):
    slow = fast = head

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    return slow

Time and Space Complexity

This makes the solution optimal.

Important Edge Cases

Always discuss these cases in interviews.

Common Interview Follow-Up Questions

This technique is reused in many linked list problems.

Summary

The Find the Middle of a Linked List problem is a classic example of how the Slow and Fast Pointer technique simplifies linked list traversal. By moving two pointers at different speeds, you can find the middle element in a single pass without extra space. Mastering this technique is essential for solving many linked list problems and performing well in coding interviews.