876. Middle of the Linked List

🟩 Easy

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.

Example 1

list

Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3.

Example 2

list

Input: head = [1,2,3,4,5,6] Output: [4,5,6] Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.

Constraints

  • The number of nodes in the list is in the range [1, 100].

  • 1 <= Node.val <= 100

Solution

My Solution

Optimal Solution (Two Pass)

Approach Analysis

This problem demonstrates two elegant approaches to find the middle node:

  1. Fast & Slow Pointer (Your Solution):

    • Uses two pointers moving at different speeds

    • Fast moves twice as fast as slow

    • When fast reaches end, slow is at middle

    • Single pass, elegant solution

  2. Two Pass Approach (Alternative):

    • First pass counts total nodes

    • Second pass moves to middle

    • More intuitive but less efficient

    • Good for learning linked lists

Visualization of Both Approaches

Fast & Slow Process (Your Solution)

Two Pass Process

Complexity Analysis

Fast & Slow Solution (Your Solution)

  • Time: O(n)

    • Single pass through list

    • Fast pointer moves n/2 times

    • Most efficient approach

  • Space: O(1)

    • Only two pointers

    • Constant extra space

    • No additional data structures

Two Pass Solution

  • Time: O(n)

    • First pass: n steps to count

    • Second pass: n/2 steps to middle

    • Total: 1.5n steps

  • Space: O(1)

    • Only two variables

    • Counter and current pointer

    • Constant extra space

Why Fast & Slow Works

  1. Mathematical Foundation:

    • Fast pointer moves at 2x speed

    • When fast reaches end

    • Slow has covered exactly half

    • Perfect for middle finding

  2. Handling Edge Cases:

    • Works for odd and even lengths

    • Automatically finds second middle

    • No special case handling needed

    • Naturally handles single node

When to Use

  1. Fast & Slow Pattern Best For:

    • Finding middle elements

    • Cycle detection

    • Pattern finding in sequences

    • Distance-based problems

  2. Two Pass Approach When:

    • Need total length anyway

    • Learning linked lists

    • Code readability priority

    • Teaching algorithms

Common Patterns & Applications

  1. Related Problems:

    • Linked List Cycle

    • Linked List Cycle II

    • Happy Number

    • Find the Duplicate Number

  2. Key Techniques:

    • Two-pointer technique

    • Floyd's Cycle Finding

    • Runner technique

    • Distance calculations

Interview Tips

  1. Solution Highlights:

    • Single pass efficiency

    • No extra space needed

    • Works for all list sizes

    • Elegant mathematical property

  2. Common Pitfalls:

    • Forgetting fast.Next check

    • Off-by-one in two-pass

    • Not handling edge cases

    • Wrong middle for even length

  3. Testing Approach:

    • Empty list

    • Single node

    • Two nodes

    • Odd length list

    • Even length list

  4. Follow-up Questions:

    • Handle circular lists?

    • Find first middle instead?

    • Return index instead of node?

    • Optimize for repeated calls?

result

Leetcode: link

Last updated

Was this helpful?