Middle of the Linked List - Leetcode Solution


πŸ’‘ Step-by-Step Thought Process

  1. Understand the problem: Find the middle node of a singly linked list; if there are two middle nodes, return the second one.
  2. Initialize two pointers, slow and fast, both starting at the head of the list.
  3. While fast and fast.next are not None:
    • Move fast two steps forward (fast.next.next).
    • Move slow one step forward (slow.next).
  4. Return slow as the middle node, as slow will be at the middle when fast reaches the end.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Problem: Middle of the Linked List

The β€œMiddle of the Linked List” problem asks us to return the middle node of a singly linked list. If the list has an even number of nodes, we are required to return the second middle node.

Example:

  • Input: 1 β†’ 2 β†’ 3 β†’ 4 β†’ 5
  • Output: Node with value 3
  • Input: 1 β†’ 2 β†’ 3 β†’ 4 β†’ 5 β†’ 6
  • Output: Node with value 4

Why This Problem Matters

This problem introduces one of the most elegant applications of the two-pointer technique β€” a method used in many advanced linked list problems. Understanding how to find midpoints with pointers builds a foundation for more complex challenges like palindrome checking, cycle detection, and list reordering.

Optimal Approach: Slow and Fast Pointer Technique

The best way to find the middle node of a singly linked list in a single pass is to use two pointers:

  • Slow pointer: moves one node at a time
  • Fast pointer: moves two nodes at a time

When the fast pointer reaches the end of the list, the slow pointer will be at the midpoint.

Steps:

  1. Initialize both slow and fast pointers at the head of the list.
  2. Traverse the list with the following loop:
    • While fast != null and fast.next != null:
      • Move slow = slow.next
      • Move fast = fast.next.next
  3. Once the loop ends, slow will be pointing to the middle node.
  4. Return slow.

Example Walkthrough

Input: 1 β†’ 2 β†’ 3 β†’ 4 β†’ 5
Iteration steps:

  • slow = 1, fast = 1
  • slow = 2, fast = 3
  • slow = 3, fast = 5
  • fast.next is null β†’ stop
  • Return slow β†’ node 3

For even-length list: 1 β†’ 2 β†’ 3 β†’ 4 β†’ 5 β†’ 6 β†’ returns node 4 (the second middle).

Time and Space Complexity

Time Complexity: O(n), where n is the number of nodes. Each node is visited at most once.
Space Complexity: O(1), as we use only two pointers.

Edge Cases to Consider

  • Single-node list β†’ return that node
  • Two-node list β†’ return the second node
  • Empty list β†’ depending on constraints, return null or handle safely

Conclusion

The β€œMiddle of the Linked List” problem is a classic use of the slow and fast pointer technique. It's a clean and efficient solution that runs in linear time without needing to count nodes or use extra memory. Mastering this strategy will benefit you in numerous other linked list-related problems.

Get Personalized Support at AlgoMap Bootcamp πŸ’‘