← Back to libraryQuestion 109 of 273
⌨️Java CodingBeginner

Find the Middle of a Linked List

📌 Definition:

Return the middle node of a singly linked list in one pass (for even length, the second of the two middles).

📖 Detailed Explanation:

Use the slow/fast (tortoise and hare) pointers: advance slow by one and fast by two each step; when fast reaches the end, slow is at the middle. O(n) time, O(1) space, one pass — no need to count length first.

💻 Solutions:
static class Node { int val; Node next; Node(int v){ val = v; } }

public static Node middle(Node head) {
    Node slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}
class Node { constructor(v) { this.val = v; this.next = null; } }

function middle(head) {
  let slow = head, fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
  }
  return slow;
}
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def middle(head):
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow
🔑 Key Points:
  • Slow moves 1, fast moves 2 per step
  • When fast hits the end, slow is the middle
  • One pass — no length pre-count
  • O(n) time, O(1) space
🌍 Real-World Example:

The two-speed pointer technique underlies cycle detection, palindrome checks on lists, and splitting a list for merge sort.

🎯 Scenario-Based Interview Question:

Why is the slow/fast approach preferred over counting the length and walking to n/2?