const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Middle of Linked List LeetCode: Fast Slow Pointer Explained

LeetCode 876 — Middle of the Linked List — is the purest fast/slow pointer demo. One pointer moves twice as fast, so when it reaches the end, the slow pointer sits exactly at the middle. Two speeds, one beautiful result.

6 min read|

Two speeds, one middle — the fast/slow pointer technique

When fast reaches the end, slow is at the middle. The simplest linked list pattern you will use everywhere.

Middle of Linked List LeetCode: The Purest Fast/Slow Pointer Demo

Middle of the Linked List (#876) is the purest fast/slow pointer demonstration on LeetCode. Two pointers start at the head. One moves one step at a time, the other moves two. When the fast pointer reaches the end, the slow pointer is sitting right at the middle. That is the entire algorithm — elegant, minimal, and worth internalizing.

The middle of linked list LeetCode problem is deceptively simple, but the technique it teaches — the tortoise and hare approach — powers a family of linked list problems. Linked List Cycle (#141), Palindrome Linked List (#234), and Sort List (#148) all depend on finding the middle or detecting structural properties using two pointers at different speeds.

In this walkthrough, you will understand why the fast/slow pointer technique works for finding the linked list midpoint, trace through both odd and even length examples, handle edge cases, and see how this building block connects to harder problems.

Understanding the Problem — Return the Middle Node

Given the head of a singly linked list, return the middle node. If there are two middle nodes (even-length list), return the second middle node. The problem is straightforward in its requirements, but the constraint of a singly linked list means you cannot index into it or know its length without traversing it first.

A naive approach would traverse the list once to count nodes, then traverse again to the middle. This works and runs in O(n) time, but it requires two passes. The fast/slow pointer technique achieves the same result in a single pass — and it introduces a pattern you will use in far more complex problems.

The key insight is that if one pointer moves at twice the speed of another, the slower pointer will have covered exactly half the distance when the faster one finishes. This is the same principle behind the tortoise and hare algorithm for cycle detection, applied here to find the midpoint.

ℹ️

Building Block

Middle of the Linked List (#876) is the building block for Palindrome Linked List — you find the middle, then reverse the second half. Master this first.

Fast/Slow Pointer — How It Finds the Middle

Initialize two pointers, slow and fast, both starting at the head of the linked list. In each iteration, slow advances one node (slow = slow.next) and fast advances two nodes (fast = fast.next.next). When fast reaches the end of the list — either fast is null or fast.next is null — slow is at the middle.

Why does this work? Think of it mathematically. If the list has n nodes, fast covers the distance in roughly n/2 iterations (since it moves 2 steps per iteration). In those same n/2 iterations, slow moves n/2 steps from the head — which is exactly the middle position. No extra counting, no second pass, no additional data structures.

The time complexity is O(n) because fast traverses the entire list once. The space complexity is O(1) — you only use two pointer variables regardless of list size. This is optimal for the problem; you cannot find the middle without looking at least at every other node.

Implementation — The Core Loop

The implementation of the fast slow pointer middle technique is remarkably concise. The entire solution fits in a handful of lines, yet it captures a principle that recurs throughout linked list problems. Here is the step-by-step approach.

Start by setting both slow and fast to the head node. Then enter a while loop with the condition: while fast is not null AND fast.next is not null. Inside the loop, move slow forward by one node and fast forward by two nodes. When the loop exits, return slow — it is pointing at the middle node.

The condition "while fast and fast.next" is carefully chosen. For odd-length lists (e.g., 5 nodes), fast will land on the last node (fast.next is null), and slow will be at position 3 — the exact middle. For even-length lists (e.g., 6 nodes), fast will land on null (it jumped past the end), and slow will be at position 4 — the second of the two middles, which is what the problem asks for.

  1. 1Set slow = head and fast = head
  2. 2While fast !== null AND fast.next !== null: move slow = slow.next, fast = fast.next.next
  3. 3When the loop exits, return slow — it points to the middle node
💡

Loop Condition

The loop condition 'while fast and fast.next' handles both even and odd lengths — for even, fast lands on null; for odd, fast lands on the last node. Slow is at middle either way.

Visual Walkthrough — Odd and Even Length Lists

Trace through [1,2,3,4,5] (odd length). Start: slow=1, fast=1. Iteration 1: slow=2, fast=3. Iteration 2: slow=3, fast=5. Now fast.next is null, so the loop exits. Slow points to node 3 — the exact middle of a 5-element list.

Now trace through [1,2,3,4,5,6] (even length). Start: slow=1, fast=1. Iteration 1: slow=2, fast=3. Iteration 2: slow=3, fast=5. Iteration 3: slow=4, fast=null (fast jumped from 5 past 6 to null). The loop exits because fast is null. Slow points to node 4 — the second middle, exactly as the problem specifies.

Notice the symmetry. In the odd case, fast stops on the last node. In the even case, fast overshoots to null. Either way, slow ends up at the correct middle position. This is not a coincidence — it is a direct consequence of the 2:1 speed ratio between the pointers.

Edge Cases — Single Node, Two Nodes, Even vs Odd

Single node: if the list has only one element, fast starts at head and fast.next is null immediately. The loop never executes. Slow stays at head, which is the middle (and only) node. This is correct behavior with no special handling needed.

Two nodes: fast starts at node 1, fast.next is node 2 (not null), so we enter the loop. Slow moves to node 2, fast moves to node 2.next which could be null. The loop exits. Slow is at node 2 — the second middle of a two-element list. Again, correct per the problem statement.

The even vs odd distinction is the only subtlety in this problem. For odd-length lists, there is one clear middle node. For even-length lists, the problem defines "middle" as the second of the two candidates. If a different problem needed the first middle, you would adjust the loop condition to "while fast.next and fast.next.next" — but for LeetCode 876, the standard condition gives the right answer.

  • Single node (n=1): return the node itself — loop never executes
  • Two nodes (n=2): return the second node — the defined "second middle"
  • Odd length: one exact middle — slow lands on it naturally
  • Even length: two middles — the standard loop returns the second one
  • Empty list: not in the constraints for #876, but would need a null check if possible
⚠️

Even-Length Lists

For even-length lists, this returns the SECOND middle node (e.g., node 4 in [1,2,3,4,5,6]). If you need the first middle, use 'while fast.next and fast.next.next' instead.

What Middle of the Linked List Teaches You

Middle of the Linked List teaches the fast/slow pointer technique for finding the midpoint of a linked list — and this technique is not limited to this one problem. Palindrome Linked List (#234) requires you to find the middle, reverse the second half, then compare both halves. Sort List (#148) uses the middle to split the list for merge sort. Both problems are significantly harder, but they start with this exact same middle-finding step.

The tortoise and hare pattern also powers cycle detection in Linked List Cycle (#141) and Linked List Cycle II (#142). In those problems, the two pointers run at different speeds to detect whether they ever meet — a variation on the same 2:1 speed ratio. Understanding why the math works here makes cycle detection feel intuitive rather than magical.

If you are preparing for coding interviews, Middle of the Linked List is one of the best problems to start with in the Linked List category. It is tagged Easy, it runs in optimal time and space, and it introduces the most important linked list technique. Drill it with YeetCode flashcards until the fast/slow pointer approach is second nature — you will need it for the medium and hard problems that build on it.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now