Skip to content
Veltoryx Technologies
FreeProblem 44● EasyLeetCode #876 ↗5 min readUpdated 30 October 2026

Middle of the Linked List

Return the middle node of a singly linked list in a single pass using fast and slow pointers.

01The problem

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes (i.e. the list length is even), return the second middle node.

Do this in a single pass with O(1) extra memory.

InputOutputWhy
[1, 2, 3, 4, 5][3, 4, 5]3 is the exact middle node (index 2 of 5)
[1, 2, 3, 4, 5, 6][4, 5, 6]Length is 6; middle nodes are 3 and 4, return second (4)
[1][1]Single node is its own middle

Constraints: 1 to 100 nodes, node values between 1 and 100.

02How to spot it

  • You need a node at a position proportional to the total length (e.g. n / 2).
  • The total length n is not known in advance.
  • The problem asks for a single pass without allocating extra collections or arrays.
  • Advance two pointers at different relative speeds: one moves twice as fast as the other. When the faster pointer reaches the tail, the slower pointer is at the midpoint.

So: fast and slow pointers (tortoise and hare).

03Pseudocode

text
slow = head ; fast = head
while fast != null and fast.next != null:
  slow = slow.next
  fast = fast.next.next
return slow
  1. Initialize slow and fast pointers at head.
  2. While fast has at least two steps available ahead, move slow by 1 and fast by 2.
  3. When fast reaches the end, slow is resting at the middle node.

04Watch it work

Visualizer · step through the algorithmlist = [1,2,3,4,5]

linked list

10
21
32
43
54
slowfast

Initialize slow and fast pointers at head (index 0, value 1).

Pseudocode

  1. slow = head ; fast = head
  2. while fast != null and fast.next != null:
  3. slow = slow.next
  4. fast = fast.next.next
  5. return slow

Variables

slow
1
fast
1
step 1 / 6

05Step-by-step dry run

  1. 1

    linked list

    10
    21
    32
    43
    54
    slowfast

    Initialize slow and fast pointers at head (index 0, value 1).

    slow = 1fast = 1

  2. 2

    linked list

    10
    21
    32
    43
    54
    slowfast

    Both fast (index 0) and fast.next (index 1) exist. Advance runners.

    slow = 1fast = 1

  3. 3

    linked list

    10
    21
    32
    43
    54
    slowfast

    Advance slow by 1 step to index 1 (2); advance fast by 2 steps to index 2 (3).

    slow = 2fast = 3

  4. 4

    linked list

    10
    21
    32
    43
    54
    slowfast

    Both fast (index 2) and fast.next (index 3) exist. Advance runners.

    slow = 2fast = 3

  5. 5

    linked list

    10
    21
    32
    43
    54
    slowfast

    Advance slow by 1 step to index 2 (3); advance fast by 2 steps to index 4 (5).

    slow = 3fast = 5

  6. 6

    linked list (middle reached)

    10
    21
    32
    43
    54
    slow

    Fast runner cannot take 2 more steps. Slow runner rests at middle node (index 2, value 3).

    middle = 3sublist = 3,4,5

06Java solution

MiddleOfLinkedList.java
public class MiddleOfLinkedList {
    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

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

        // Traverse while fast can safely advance two steps
        while (fast != null && fast.next != null) {
            slow = slow.next;       // 1 step
            fast = fast.next.next;  // 2 steps
        }

        return slow; // fast at end -> slow at middle
    }

    public static void main(String[] args) {
        // Test 1: Odd length [1, 2, 3, 4, 5]
        ListNode head1 = new ListNode(1);
        head1.next = new ListNode(2);
        head1.next.next = new ListNode(3);
        head1.next.next.next = new ListNode(4);
        head1.next.next.next.next = new ListNode(5);

        ListNode mid1 = middleNode(head1);
        printList(mid1); // 3 4 5

        // Test 2: Even length [1, 2, 3, 4, 5, 6]
        ListNode head2 = new ListNode(1);
        head2.next = new ListNode(2);
        head2.next.next = new ListNode(3);
        head2.next.next.next = new ListNode(4);
        head2.next.next.next.next = new ListNode(5);
        head2.next.next.next.next.next = new ListNode(6);

        ListNode mid2 = middleNode(head2);
        printList(mid2); // 4 5 6
    }

    private static void printList(ListNode node) {
        ListNode curr = node;
        while (curr != null) {
            System.out.print(curr.val + (curr.next != null ? " " : ""));
            curr = curr.next;
        }
        System.out.println();
    }
}
Expected output
3 4 5
4 5 6

Tricky lines, decoded

  • while (fast != null && fast.next != null): Both checks are mandatory. For odd lengths, fast.next == null stops the loop at the tail node. For even lengths, fast == null stops the loop after fast steps beyond the tail.
  • return slow: Starting both at head naturally causes an even-length list to return the second middle node (index n / 2).

07Why it works

At any point after k loop iterations, slow has traversed k steps and fast has traversed 2k steps.

  • For an odd-length list with 2m + 1 nodes, fast lands on the last node (index 2m) after m iterations. slow is at index m, the exact middle.
  • For an even-length list with 2m nodes, fast steps out to null after m iterations. slow is at index m, which is the second of the two middle elements.

Invariant: After k steps, slow is at node index k and fast is at node index 2k.

08Time and space

  • Time: O(n). fast visits each node at most once, taking n / 2 iterations.
  • Space: O(1). Only two pointer references are maintained.

09Edge cases

InputOutputWhy it matters
[1][1]Single node is its own middle
[1, 2][2]Two nodes; returns the second middle
[1, 2, 3][2, 3]Minimal odd-length list
[1, 2, 3, 4, 5, 6][4, 5, 6]Even length returns second middle
[1, 2, 3, 4, 5][3, 4, 5]Odd length returns exact middle

10Common mistakes

  • Only checking fast.next != null. If list length is even, fast advances to null, and evaluating fast.next in the next iteration throws a NullPointerException.
  • Starting fast at head.next. This shifts slow one step earlier, returning the first middle instead of the required second middle for even lengths.
  • Copying nodes into an array. Allocates O(n) auxiliary memory when two pointers solve it in O(1).

11Other approaches

ApproachTimeSpaceUse when
Two-pass countO(n)O(1)First pass counts n, second walks n / 2
Fast & slowO(n)O(1)Default single-pass interview solution
Copy to arrayO(n)O(n)Only if repeated index lookups needed

12In the interview

  • Where is this pattern applied?
    • Palindrome Linked List (Problem 49): Splitting the list in half before reversing the second half.
    • Linked List Sort (Merge Sort on Lists): Splitting a list into two equal halves.
    • Reorder List (LeetCode 143): Finding the split point to interweave nodes.

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. Why is it necessary to check both fast != null and fast.next != null in the loop condition?

  2. 2. How would you adjust the algorithm if an interviewer specifically asks for the first middle node on an even-length list?

14Solve it yourself

Apply this two-pointer strategy to LeetCode 143 (Reorder List):

  1. Find the middle node using slow and fast pointers.
  2. Reverse the second half of the list in place.
  3. Merge the first half and reversed second half by alternating nodes.