Middle of the Linked List

Easy
Fast & Slow Pointers

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.

This problem elegantly demonstrates the fast & slow pointer technique. When the fast pointer reaches the end, the slow pointer will be at exactly the middle. Think of it like two runners on a track - when the faster one finishes, the slower one is at the halfway point.

Example 1

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

Example 2

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
Show Hints (4)
Hint 1: If you walk through the list once to count nodes, then walk halfway, that works but requires two passes. Can you do it in one pass?
Hint 2: What if one pointer moves twice as fast as another?
Hint 3: When the fast pointer has traversed the entire list, where would a pointer moving at half the speed be?
Hint 4: For even-length lists, why does stopping when fast.next is null give us the second middle?
Loading editor...

Test Results

Click "Run" to execute your code against test cases

AI Tutor

Socratic guidance - I'll ask questions, not give answers

AI Tutor
Hello! I'm your AI tutor, here to guide you through this problem using the Socratic method. I won't give you direct answers, but I'll ask questions and provide hints to help you discover the solution yourself. What's your first instinct when you look at this problem? What approach comes to mind?