Find the Middle of a Linked List (No length)

Data Structures
Easy
Google
65.3K views

Find the middle node of a singly linked list in one pass without knowing the list's total length in advance. Use the two-pointer approach.

Why Interviewers Ask This

Google interviewers ask this to evaluate a candidate's ability to optimize space and time complexity simultaneously. They specifically test if you recognize that calculating length requires two passes, whereas the fast-slow pointer technique solves it in one pass with O(1) extra space. This assesses your fundamental grasp of algorithmic efficiency and pointer manipulation skills.

How to Answer This Question

1. Clarify constraints immediately: Confirm the list is singly linked and that you cannot traverse it twice or use recursion due to stack depth limits. 2. Propose the Two-Pointer Strategy: Explain that you will use a slow pointer moving one step and a fast pointer moving two steps per iteration. 3. Define the Loop Condition: State clearly that the loop continues while the fast pointer and its next node are not null. 4. Handle Edge Cases: Briefly mention how this works for even-length lists (returning the second middle node) versus odd-length lists. 5. Analyze Complexity: Conclude by explicitly stating the Time Complexity is O(n) and Space Complexity is O(1), demonstrating awareness of Google's focus on resource efficiency.

Key Points to Cover

  • Explicitly state the O(1) space complexity advantage over calculating length first
  • Demonstrate understanding of the fast pointer moving two steps while slow moves one
  • Correctly handle the loop termination condition (fast != null && fast.next != null)
  • Clarify behavior for even-length lists (returning the second middle node)
  • Connect the solution to Google's emphasis on optimizing for both time and space

Sample Answer

To find the middle of a singly linked list without knowing its length, I would implement the classic 'Fast and Slow' pointer approach, which allows us to solve this in a single traversal. First, I initialize two pointers…

Common Mistakes to Avoid

  • Attempting to count nodes first, which requires two passes and violates the 'one pass' constraint
  • Using recursion to find the middle, which introduces O(n) stack space overhead
  • Incorrectly handling the loop condition, causing an infinite loop or off-by-one errors
  • Failing to address what happens when the list contains zero or one node

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 166 Data Structures questionsBrowse all 145 Google questions