Find the Middle of a Linked List (No length)

Data Structures
Easy
Google
65.2K 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, both starting at the head of the list. The slow pointer will advance one node at a time, while the fast pointer advances two nodes at a time. As we iterate through the loop, checking if the fast pointer and its subsequent node exist, the fast pointer essentially moves twice as fast as the slow pointer. Consequently, when the fast pointer reaches the end of the list, the slow pointer will naturally be positioned exactly at the middle node. For example, if the list has five nodes, the fast pointer traverses to the end in three jumps, placing the slow pointer at index two, which is the correct middle. If the list has an even number of nodes, say four, the fast pointer will hit null after the fourth node, leaving the slow pointer at the third node, which is standard behavior for returning the second middle element. This solution achieves O(n) time complexity since we visit each node once, and crucially, it uses O(1) space complexity because we only store two references regardless of list size. This aligns perfectly with the efficiency standards expected at Google, avoiding unnecessary memory allocation or multiple passes.

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

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 87 Google questions