15% off all plans & roadmaps Use code

See plans

Middle of the Linked List

Updated

TL;DR: You need the middle node of a singly linked list, and you cannot index into a linked list. Move a slow pointer one node at a time and a fast pointer two nodes at a time, and the slow pointer stops at the middle in one pass.

Problem

You receive the head of a singly linked list. Return the node that sits in the middle of it.

When the list has an even number of nodes, two nodes compete for the middle. In that case, return the later of the two.

Example 1

Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The list has five nodes, so node 3 is the one middle node.

Example 2

Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: With six nodes, the middle pair is 3 and 4. The answer is 4, the second of the two.

Constraints

  • The list holds between 1 and 100 nodes.
  • Each Node.val is between 1 and 100.

Constraints that matter

  • A list of up to 100 nodes is small, so the two-pass method passes, but the one-pass method is what interviewers expect.
  • You cannot read a node by index, so the position must come from walking the list.
  • Values run from 1 to 100, but the answer depends only on position. Never compare values to find the middle.
  • The even-length rule means the stop condition must land on the second middle, not the first.

View on LeetCode

Hints

Hint 1

The problem asks for a position, but a linked list gives no direct way to reach position k.

Hint 2

The naive route counts the nodes first.

Solution

Intuition

The problem asks for a position, but a linked list gives no direct way to reach position k. You learn where the middle is only by walking, and the length is the missing piece. The pattern that handles this is two pointers moving at different speeds in a single pass.

Picture two runners starting together on a track, one running twice as fast. When the fast runner reaches the finish, the slow runner is at the halfway mark, which is the middle node.

Notice the ratio. If one pointer moves two nodes for every node the other moves, the slow pointer is at the midpoint when the fast pointer reaches the end. The fast pointer does the counting for you, so you never need the length.

This works for odd and even lengths. The loop stops when the fast pointer cannot take a full double step. At that point the slow pointer sits at index n // 2, which is the second middle for even lengths.

Watch the pattern animation

Worked example

Input: head = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Output: [10, 11, 12, 13, 14, 15]
Explanation: The list has 11 nodes, so the middle sits at zero-based index 5. That node holds 10. The result starts at 10 and runs to the tail, which is the same node chain as the input.

Approach 1: Brute force

The naive route counts the nodes first. You walk the whole list to get n, then walk again n // 2 steps to reach the middle. The first walk exists only to tell you where to stop on the second walk. The list is at most 100 nodes, so this passes, but every node is read twice.

Steps

  1. Walk from head to the end and count the nodes into n.
  2. Compute n // 2, the zero-based index of the second middle.
  3. Point a cursor back at head.
  4. Advance the cursor n // 2 times.
  5. Return the cursor.

Dry run

Start with the 11-node example. The first walk visits all 11 nodes, so n = 11, and n // 2 = 5. The cursor starts at head and moves 5 times through 6, 7, 8, 9, and 10. It stops on 10, so the output is [10, 11, 12, 13, 14, 15].

Complexity

TimeO(n)
SpaceO(1)

Let n be the number of nodes. The counting loop visits each node once, and the second loop moves the cursor at most n // 2 times. The total is n + n // 2, which is still linear. The code stores only a counter and a cursor, so the extra space is constant.

PythonTested in our runner

class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # First pass: count the nodes
        n = 0
        cur = head
        while cur:
            n += 1
            cur = cur.next
        # Second pass: walk to index n // 2
        cur = head
        for _ in range(n // 2):
            cur = cur.next
        return cur

The Fixed Separation (Nth Node from End) infographic is part of Pro

A one-page visual of the pattern: when it applies, the steps and the invariant to keep.

Unlock the infographic

Solution pattern

Fixed Separation (Nth Node from End)

Part of Two Pointers

Approach 2: Fast and slow pointersOptimalFixed Separation (Nth Node from End)

What's slow in the brute force is the second walk. The insight that fixes it is that you never need n before you move. Let one cursor cover two nodes for every node the other covers. When the fast cursor reaches the end, the slow cursor has covered half the list and sits on the middle.

Why it is correct. At every step, the slow pointer has moved exactly half as many nodes as the fast pointer. So when fast cannot take a full double step, slow sits at index n // 2, which is the middle. This holds for both odd and even lengths, so the answer is always the correct middle.

Steps

  1. Set slow and fast both to head.
  2. While fast and fast.next both exist, move slow one node forward.
  3. In the same iteration, move fast two nodes forward.
  4. When the loop stops, slow holds the answer.
  5. Return slow.

Dry run

The table traces the 11-node example. Each row is one check of the loop condition, followed by the moves it causes.

Stepslowfastfast and fast.next exist?Action
Start55yesBegin
167yesslow moves 1, fast moves 2
279yesslow moves 1, fast moves 2
3811yesslow moves 1, fast moves 2
4913yesslow moves 1, fast moves 2
51015no, fast.next is nullStop

The loop ends with slow on 10. The output is [10, 11, 12, 13, 14, 15].

Complexity

TimeO(n)
SpaceO(1)

Let n be the number of nodes. The fast pointer advances two nodes per iteration, so the loop runs at most n / 2 times, with constant work each time. Slow moves one node per iteration, so it reaches the middle exactly when fast reaches the end. The only storage is the two pointers.

PythonTested in our runner

class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = head
        fast = head
        # fast needs two nodes ahead to take a double step
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

Common mistakes

  • Checking while fast.next.next without checking fast first. For a one-node list, fast.next is None and the access crashes. Test fast and fast.next before touching fast.next.next.
  • Using while fast and fast.next.next. For six nodes this stops at 3, the first middle. Check only fast and fast.next so the loop reaches the second middle.
  • Writing fast = fast.next instead of fast = fast.next.next. Both pointers then move at the same speed, and slow never leaves the head.
  • Writing range(n / 2) in Python. Division returns a float there, and range rejects it. Use n // 2.
  • Comparing fast.val to a stop value instead of the pointer. Values repeat, so a value match can stop the loop on a real middle node. Compare pointers to null.
  • Starting slow at head.next in the one-pass method. For a two-node list that returns the second node by accident but breaks the odd-length rule for other sizes. Start both pointers at head.

Edge cases

  • A single node, [7]: the loop never runs, so the answer is that node.
  • Two nodes, [1,2]: slow moves to 2 on the first step and fast becomes null, so the answer is 2, the second middle.
  • Three nodes, [1,2,3]: the answer is 2, the exact center.
  • Six nodes, [1,2,3,4,5,6]: the answer is 4, the second of the two middles.
  • Repeated values, [4,4,4,4]: the answer is the third node. Positions decide the result, not values.
  • Maximum size of 100 nodes: the fast loop runs at most 50 times, and the brute force does about 150 node visits.

Follow-ups interviewers ask

Follow-ups are part of Pro

The questions interviewers ask after you solve Middle of the Linked List, with how to answer them.

Unlock the follow-ups

Middle of the Linked List FAQ

What is the time complexity of Middle of the Linked List?

The one-pass fast and slow solution runs in O(n) time, where n is the number of nodes. The two-pass brute force also runs in O(n) time, with a larger constant factor.

Why use fast and slow pointers for Middle of the Linked List?

A linked list has no index, so the midpoint is unknown until you walk. A fast pointer moving twice as fast as a slow pointer reaches the end when the slow pointer is at the middle. This removes the need to count nodes first.

Can Middle of the Linked List be solved in O(1) extra space?

Yes. The fast and slow pointer solution stores only two node references. The brute force stores one counter and one cursor, so it is also constant space.

Is the two-pass counting method wrong?

No. It returns the correct node for every valid input. It reads each node twice, so it is slower in practice, and interviewers usually expect the one-pass version.

Asked at

See which companies ask this — Pro

Practise it