beginner9 min read·Updated September 23, 2026

Middle of a Linked List Explained: Tracing 1→2→3→4 vs 1→2→3→4→5

Master finding the middle of even and odd linked lists using the two-pointer mental model. Walk through the 1→2→3→4 and 1→2→3→4→5 examples step by step.

By Learnisim AI·Published September 23, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • Basic understanding of pointers and references
  • Familiarity with singly linked list structure
Two-Pointer Linked List Middle Finder (1 → 2 → 3 → 4) Fast moves 2x speed; Slow lands exactly on second middle (node 3) for even length Phase 1: Start at Head (Slow = Head, Fast = Head) Val: 1 Val: 2 Val: 3 Val: 4 None slow & fast Phase 2: Traversal Complete (fast reaches None → slow stops at Node 3) Val: 1 Val: 2 Val: 3 (Mid) Val: 4 None slow fast Step 1: slow=2, fast=3 Step 2: slow=3, fast=None (Loop terminates!) Phase 3: Python Implementation & Critical Conditions def middleNode(head: ListNode) -> ListNode: slow, fast = head, head while fast and fast.next: # Check BOTH to avoid None error slow, fast = slow.next, fast.next.next ⚠️ Common Syntax Pitfalls: • Forgetting fast.next causes AttributeError • Writing fast.next instead of fast.next.next = loop
Middle of 1→2→3→4 and of 1→2→3→4→5 overview diagram
Why

Why We Need a Smarter Way to Find the Middle of a Linked List

Imagine you are handed a physical scavenger hunt where each clue is written on a slip of paper, and each slip only tells you where to find the very next slip. You have no map, no index numbers, and no way to jump straight to item number three. If you want to find the exact middle of this chain—such as our working example of even length —how would you do it? In an array, finding the middle is trivial because you can just compute length divided by two and jump directly to that index. But in a singly linked list, nodes only know their successor, meaning finding the total length requires walking the entire chain from start to finish before you even begin looking for the midpoint.
Why Finding the Middle of a Linked List is Tricky No index numbers. Each node only knows its successor. Can't jump to item #3! Array (Random Access) Direct jump: length / 2 A[0] 10 A[1] 20 A[2] Mid (30) A[3] 40 A[4] 50 O(1) Instant Jump Singly Linked List: Even Length Scavenger Hunt (1 → 2 → 3 → 4) To find the middle, you must walk the entire chain from start to finish before you even know the length! Node 1 Val: 1 Next ➔ Node 2 (Mid 1) Val: 2 Next ➔ Node 3 (Mid 2) Val: 3 Next ➔ Node 4 (Tail) Val: 4 Null The Even Length Dilemma (4 items) Length = 4. Midpoint can be Node 2 (value 2) or Node 3 (value 3). Standard convention prefers right-mid. The Solution: Tortoise & Hare Algorithm Slow pointer takes 1 step, fast pointer takes 2 steps. When fast reaches the end, slow lands exactly on the middle!
Why We Need a Smarter Way to Find the Middle of a Linked List diagram
Model

The Two-Pointer Model for Linked List Middles

Phase 2: The Two-Pointer Model

When you look at our linked lists and , your eyes instantly find node 3. But a pointer cannot jump directly to an index because linked list nodes only know their immediate neighbors. To solve this without counting the total length first, we deploy two pointers from the start: a slow pointer and a fast pointer.
Imagine both pointers standing side by side at node 1. On each step of our journey, the slow pointer moves forward by one node, while the fast pointer jumps forward by two nodes. Because the fast pointer travels at twice the speed, it reaches the end of the list precisely when the slow pointer arrives at the halfway point. For our even-length list , this dual-speed race guarantees that when the fast pointer clears the list, the slow pointer lands exactly on the second middle node, node 3.
Two-Pointer Model: Finding the Linked List Middle (Even Length 1→2→3→4) Start: Both pointers begin at Node 1 Node 1 Node 2 Node 3 Node 4 NULL Slow (1x) Fast (2x) Final State: Fast pointer reaches NULL → Slow pointer lands precisely on Node 3 (the second middle!) Node 1 visited Node 2 visited Node 3 ★ SLOW LANDS HERE Node 4 NULL FAST CLEARS LIST Slow Pointer (1x speed) Why does this work perfectly for even lengths (4 nodes)? • Fast pointer travels at 2× speed, covering twice the distance of the slow pointer in the exact same time. • When Fast jumps past Node 4 into NULL (2 steps per node × 2 hops = 4 total steps), Slow has taken 2 hops. • 2 hops from Node 1 lands Slow directly on Node 3, the standard convention for even-length middle extraction!
The Two-Pointer Model for Linked List Middles diagram
Syntax

Syntax and Implementation for the Two-Pointer Middle Finder

Phase 3: Syntax and APIs

Now that we have visualized how our slow and fast pointers march down the list, we need to translate that mental model into precise programming syntax. For our locked example of finding the middle of or , we declare both pointers at the head of the list.
In Python, a standard singly-linked list node class paired with the traversal loop looks like this:
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def middleNode(head: ListNode) -> ListNode:
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow
A common syntax mistake is forgetting to check both fast and fast.next in the while loop condition. If you only check while fast:, trying to access fast.next.next when fast is the final node will raise an AttributeError because you attempt to access next on a None type.
Another frequent bug is writing fast = fast.next instead of fast.next.next inside the loop body, which accidentally turns your fast pointer into a slow pointer and traps your code in an infinite loop.
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def middleNode(head: ListNode) -> ListNode:
    # Complete the traversal loop for 1->2->3->4
    slow = head
    fast = head
    while _________:
        slow = slow.next
        fast = _________
    return slow
Worked example

Tracing the Two-Pointer Traversal on Even and Odd Linked Lists

Phase 4: Traced Execution

Let us run our slow and fast pointer syntax through our locked working examples: the even list 1→2→3→4 and the odd list 1→2→3→4→5. Watch how the loop condition and step sizes dictate the final position of the slow pointer.

Given:

- Even List: head = 1 -> 2 -> 3 -> 4 -> null
- Odd List: head = 1 -> 2 -> 3 -> 4 -> 5 -> null

Steps for Even List (1 -> 2 -> 3 -> 4):

1. Initialization: slow = 1, fast = 1
2. Iteration 1: fast and fast.next exist.
- slow moves to 2
- fast moves to 3 ()
- State: slow = 2, fast = 3
3. Iteration 2: fast (3) and fast.next (4) exist.
- slow moves to 3
- fast moves to null ()
- State: slow = 3, fast = null
4. Termination: fast is null, so fast.next check fails. Loop ends.

Steps for Odd List (1 -> 2 -> 3 -> 4 -> 5):

1. Initialization: slow = 1, fast = 1
2. Iteration 1: fast and fast.next exist.
- slow moves to 2
- fast moves to 3
- State: slow = 2, fast = 3
3. Iteration 2: fast (3) and fast.next (4) exist.
- slow moves to 3
- fast moves to 5 ()
- State: slow = 3, fast = 5
4. Termination: fast (5) exists, but fast.next is null. The while loop condition fast.next fails. Loop ends.

Result:

•Even List returns node with value 3 (the second middle).
•Odd List returns node with value 3 (the exact middle).
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def middleNode(head: ListNode) -> ListNode:
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow
Phase 4: Traced Execution (Even vs Odd Lists) Tracking slow (1x) and fast (2x) pointers to termination Even List: 1 → 2 → 3 → 4 → null 1 2 3 4 null slow (val: 3) fast = null Even Termination & Result: fast hits null. Loop ends. slow points to 3 (2nd middle). Odd List: 1 → 2 → 3 → 4 → 5 → null 1 2 3 4 5 null slow (val: 3) fast at node 5 Odd Termination & Result: fast.next is null. Loop ends. slow at 3 (exact middle). Core Loop Condition: while fast and fast.next: • Even Length: fast successfully reaches null. slow lands on the second middle node (3). • Odd Length: fast lands on the last node (5), but fast.next is null. The while check fails, leaving slow precisely on the exact middle node (3).
Tracing the Two-Pointer Traversal on Even and Odd Linked Lists diagram
Practice

Practice Writing the Two-Pointer Middle Finder

Now that you have traced how the slow and fast pointers navigate through our locked working example of 1 2 3 4 and 1 2 3 4 5, it is time to write the code yourself. In this practice exercise, you will complete a function that initializes both slow and fast pointers at the head of the list and advances them correctly. Remember the crucial loop condition from our syntax phase: fast must advance two steps while fast.next is valid, allowing slow to land precisely on the second middle node for even lengths. Fill in the missing traversal loop below to make the function return the correct middle node.
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def middleNode(head: ListNode) -> ListNode:
    slow = head
    fast = head
    # TODO: write the while loop using fast and fast.next
    
    return slow
Apply

Applying the Two-Pointer Pattern to Neighboring LinkedList Problems

Phase 6: Applying the Fast and Slow Template

You have mastered finding the second middle of an even list like (returning node 3) and the single middle of an odd list like (returning node 3) using the slow and fast pointer cadence. The real power of this traversal pattern is that its core mechanism—differential pacing—solves structural problems in linear time without needing extra memory or list length pre-calculations. Consider how you might adapt this exact mental model to detect a cycle in a linked list or to find the -th node from the end. By keeping your slow and fast initialization and loop condition identical to your middle finder, you can build entire families of pointer-based list algorithms.
When faced with a new singly linked list challenge, ask yourself: Can I use one pointer moving ahead of another to gather positional information in a single pass? If the problem requires finding a relative midpoint, a split point, or a convergence point, the two-pointer template you built for is your direct blueprint.
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

# Given the middle finder template:
# def middleNode(head):
#     slow = fast = head
#     while fast and fast.next:
#         slow = slow.next
#         fast = fast.next.next
#     return slow

# TRANSFER QUESTION:
# How would you modify the loop condition or pointer movement above
# to check if the linked list contains a cycle (where a node's next pointer
# loops back to a previous node) instead of finding the middle?

FAQ

What is the result of the two-pointer middle finder on the even list 1→2→3→4?
For an even-length list like 1→2→3→4, standard implementations using the slow and fast pointer approach return the second middle node (node 3) as the expected result.
How does the two-pointer technique handle both even and odd linked lists?
The fast pointer moves two steps at a time while the slow pointer moves one step. When the fast pointer reaches the end of the list, the slow pointer rests exactly at the middle node for both odd lengths and the upper-middle node for even lengths.
What is the time and space complexity of finding the middle of a linked list?
The time complexity is O(N) because we traverse the list in a single pass, and the space complexity is O(1) as we only use a few pointer variables.

Keep learning