LeetCode / Classic Linked List · Slow/Fast Pointers Easy

Linked List — Middle Node (Slow/Fast Pointers)

LC
Practice this on LeetCode — "Middle of the Linked List" is the classic single-pass midpoint problem.
Open Problem

Find the middle node in one pass: slow moves one step, fast moves two. When fast reaches the end, slow sits on the middle. O(N) time, O(1) space

What does it solve?

Find the middle node of a linked list. The obvious way needs two passes: count the length N, then walk N/2 again. The slow/fast trick finds it in a single pass, without ever knowing the length up front.

Key insight: move slow one node and fast two nodes each step. Fast covers the list twice as quickly, so when fast falls off the end, slow has travelled exactly half the distance: it's on the middle. For an even length the loop condition decides which of the two middles you land on: while fast && fast.next gives the second middle (LeetCode's answer).

The core idea

// slow lands on the middle
slow = head, fast = head
while fast && fast.next:
  slow = slow.next
  fast = fast.next.next
return slow

Complexity

Time complexity
O(N)

Fast reaches the end in about N/2 steps, and that's the whole run. One pass, no length precomputation.

Space complexity
O(1)

Two pointers, nothing else. No array copy, no length counter beyond the pointers themselves.


Lists & edge cases

Live animation — fast races ahead; when it ends, slow is the middle
slow (1 step) fast (2 steps) both on same node middle found
Linked list
Pointer positions
slow = — fast = — steps = — middle = —
Step — of —
slow at
—
+1 per step
fast at
—
+2 per step
steps
—
taken
middle
—
slow's node
Select a list and press Play to start.
✓ Done
Speed Medium

Step-by-step table

Step slow idx slow val fast idx fast val fast ended?

C++ implementation

middle_of_list.cpp
#include <bits/stdc++.h>
using namespace std;

struct ListNode { int val; ListNode* next; };

// Slow/fast: when fast falls off the end, slow is the middle.
ListNode* middleNode(ListNode* head) {
    ListNode* slow = head;
    ListNode* fast = head;

    while (fast && fast->next) {
        slow = slow->next;         // one step
        fast = fast->next->next;   // two steps
    }
    return slow;                  // even length -> the SECOND middle
}

My Notes

📝 Notes ✅

▼