Find Middle of Linked List | Java Fresher Interview
Fast recall
Quick revision
Core answer
slow one node and fast two nodes per step; when fast reaches the end, slow is at the middle - all in one pass, O(n) time, O(1) space. For an even-length list this lands on the second middle (use fast.next != null to stop earlier if you want the first).Whether you know the one-pass two-pointer trick instead of counting length then re-traversing
Doing two passes (count then walk to n/2), or mishandling even-length and null checks
Control which middle you return for even lengths via the fast-pointer loop condition
Interview-ready guide
Interview answer
The clean way to find the middle of a linked list is the slow and fast pointer technique, sometimes called tortoise and hare.
I start both pointers at the head. Then in each step I move the slow pointer one node forward and the fast pointer two nodes forward. The fast pointer covers ground twice as quickly, so by the time it reaches the end of the list, the slow pointer has only gone halfway, which means it's sitting right at the middle.
That gives me the answer in a single pass, O of n time and O of one space. The alternative is to first count the length and then walk again to position length over two, but that's two passes, and the two-pointer version is what interviewers want. A couple of details I'm careful about.
The loop condition has to be fast not null and fast dot next not null, checked before I move fast twice, otherwise I'll hit a null pointer exception when I try fast dot next dot next near the end. For an odd-length list, slow lands on the single, unambiguous middle. For an even-length list there are two middle nodes, and which one I return depends on my loop condition.
With fast not null and fast dot next not null, I return the second middle; if I want the first, I tweak the condition. I'd also handle the empty list by returning null when head is null. And I'd point out this two-speed idea is the same trick used to detect cycles in a linked list.
For example, in 1 -> 2 -> 3 -> 4 -> 5, slow moves through 2, 3 while fast moves through 3, 5. Fast cannot take another two-node step, so slow is left on the middle node 3.
Find Middle of Linked List | Java Fresher Interview
Direct Answer
Use the slow/fast (tortoise-and-hare) pointers. Move slow one node and fast two nodes per step; when fast reaches the end, slow is at the middle - all in one pass, O(n) time, O(1) space. For an even-length list this lands on the second middle (use fast.next != null to stop earlier if you want the first).
Deep Dive & Architecture
Place slow and fast at the head. Move slow by one node and fast by two while fast can take a full two-node step. When fast reaches the end, slow has covered half as much distance and stands at the middle.
Worked example: 1 -> 2 -> 3 -> 4 -> 5
The pointer pairs after each move are (2, 3) and (3, 5). Fast cannot advance two more nodes, so slow is at node 3, the middle.
Even-length lists need a stated convention. With 1 -> 2 -> 3 -> 4 and the common loop while (fast != null && fast.next != null), slow stops at node 3, the second middle. If the first middle is required, use a condition that stops before fast crosses the last pair, or keep a previous-slow reference.
The one-pass method takes O(n) time and O(1) extra space. Counting nodes first and walking halfway is also O(n), but it needs two passes. Be careful to check both fast and fast.next before moving fast twice.
This movement rule is also the foundation of Floyd's cycle algorithm, but middle-finding assumes the list is acyclic; otherwise the loop does not reach an end.
Code Example
class Node { int val; Node next; Node(int v){ val = v; } }
Node middle(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // +1
fast = fast.next.next; // +2
}
return slow; // middle (2nd middle if even length)
}
// 1->2->3->4->5 : slow 1->2->3, fast 1->3->5(end) => middle 3
// 1->2->3->4 : slow 1->2->3, fast 1->3->null => middle 3 (second)
Common Mistakes & Pitfalls
- Two passes: counting then re-walking works but isn't the expected one-pass answer.
- NPE on fast.next.next: guard with
fast != null && fast.next != nullbefore stepping fast twice. - Even-length confusion: be explicit about returning the first vs second middle.
- Empty list: handle
head == null(return null).
Check your understanding
Answer these without looking back at the article.
- How do you detect a cycle in a linked list?
- How do you reverse a singly linked list in Java?
- What is a linked list and how does it differ from an array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.