Find the Middle of a Linked List
Table of Contents + β
This one looks small. But the interviewer is watching for something. Can you walk a linked list cleverly, without counting it twice? A linked list does not tell you its size. So finding the middle feels tricky. That is the whole point. The interviewer wants to see if you reach for the famous fast and slow pointer trick instead of doing extra work.
π― The Problem
You are given the head of a singly linked list. You have to return the middle node. If the list has an even number of nodes, there are two middle nodes. In that case you return the second one.
A singly linked list is a chain of nodes. Each node holds a value and a pointer to the next node. The last node points to nothing.
Input: 1 -> 2 -> 3 -> 4 -> 5Output: 3 (the middle node)
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6Output: 4 (two middles: 3 and 4, we return the second one)π’ The Simple Way First
Let us think out loud, the way you would in the room.
The easy idea is to count first. You walk the whole list once and count how many nodes there are. Say you get n nodes. Then the middle is at position n / 2. So you walk again from the start and stop after n / 2 steps. That node is your answer.
This works. The result is correct. But see the problem? You walk the list twice. One pass to count. One pass to reach the middle. The interviewer will nod and then ask the real question: βCan you do it in a single pass?β
That is where the better idea comes in.
π’π The Optimal Way: Fast and Slow Pointers
Here is the smart trick. You use two pointers that start at the head.
- The slow pointer moves one step at a time.
- The fast pointer moves two steps at a time.
Now think about the speed. The fast pointer moves twice as fast as the slow one. So when the fast pointer reaches the end of the list, the slow pointer has only gone half the way. And half the way is the middle. That is it.
People call this the tortoise and hare trick. The slow pointer is the tortoise. The fast pointer is the hare. The hare finishes the track. The tortoise is standing right at the middle.
Think of two friends walking on the same road. Riya walks one step at a time. Her friend walks two steps in the same time. When the friend reaches the end of the road, Riya is exactly halfway down it. You did not measure the road. You just used speed.
Note
For an even-length list, this naturally lands the slow pointer on the second middle node. That matches what most interviewers ask for. If they want the first middle instead, you stop one step earlier. Always ask which one they want.
Steps to Find the Middle
- Start both
slowandfastat the head of the list. - Loop while
fastis not null andfast.nextis not null. - Inside the loop, move
slowforward by one node. - In the same step, move
fastforward by two nodes. - When the loop ends,
slowis sitting on the middle node. Return it.
# A node in the singly linked listclass Node: def __init__(self, data): self.data = data self.next = None
# Find the middle node using fast and slow pointersdef find_middle(head): slow = head fast = head # Move fast by two and slow by one while fast is not None and fast.next is not None: slow = slow.next fast = fast.next.next return slow # slow is now the middle
# Build 1 -> 2 -> 3 -> 4 -> 5head = Node(1)head.next = Node(2)head.next.next = Node(3)head.next.next.next = Node(4)head.next.next.next.next = Node(5)
mid = find_middle(head)print("Middle value:", mid.data)The output of the above code will be:
Middle value: 3β±οΈ Time and Space Complexity
Let us compare the two ideas side by side.
| Approach | Time | Space | Passes |
|---|---|---|---|
| Count then walk again | O(n) | O(1) | Two passes |
| Fast and slow pointers | O(n) | O(1) | One pass |
Both are O(n) time and O(1) space. So why does the interviewer prefer the second one? Because it does the job in a single pass. No counting. No second loop. The code is clean and it shows you know the pointer trick.
Tip
The same fast and slow pointer idea solves many linked list problems. You use it to detect a cycle. You use it to find the start of a loop. So once you learn this trick here, you carry it into harder questions. Say that out loud in the interview. It shows you see the bigger pattern.
π§© Key Takeaways
- β A singly linked list does not store its length, so finding the middle is not as simple as it looks in an array.
- β The counting method works but needs two passes through the list.
- β The fast and slow pointer trick finds the middle in one pass: slow moves one step, fast moves two.
- β When the fast pointer hits the end, the slow pointer is exactly at the middle.
- β For an even-length list, the slow pointer lands on the second middle node. Ask the interviewer which middle they want.
- β This is O(n) time and O(1) space, and the same trick powers cycle-detection problems too.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
In the fast and slow pointer method, how many steps does the fast pointer take for every one step the slow pointer takes?
Why: The fast pointer moves two nodes while the slow pointer moves one, so it travels twice as fast.
- 2
Where is the slow pointer when the fast pointer reaches the end of the list?
Why: Since fast moves twice as fast, slow has covered exactly half the list, which is the middle.
- 3
For a list with an even number of nodes, which middle does the standard loop return?
Why: With the loop condition checking fast and fast.next, the slow pointer ends on the second of the two middle nodes.
- 4
What is the space complexity of the fast and slow pointer solution?
Why: It uses only two pointers no matter how long the list is, so the extra space is constant.