Find the Middle of a Linked List

You are given the head pointer of a singly linked list. Return the value of the middle node. Examples serialize the linked list values from head → tail. Using…

You are given the head pointer of a singly linked list. Return the **value of the middle node**. Examples serialize the linked list values from **head → tail**. Using 0-based indexing, the middle index is `floor(n / 2)`, where `n` is the number of nodes. When the list has an **even** number of nodes there are two middle nodes — return the **second** one (the node at index `floor(n / 2)`). For example, `[1, 2, 3, 4, 5]` is 1 → 2 → 3 → 4 → 5; the middle index is `floor(5 / 2) = 2`, whose value is `3`. For `[1, 2, 3, 4, 5, 6]` the middle index is `floor(6 / 2) = 3`, whose value is `4`.

- `1 <= n <= 10^5`, where `n` is the number of nodes - `-10^9 <= node.val <= 10^9` - The list is a proper singly linked list with no cycles.

Example 1: input head = [1,2,3,4,5]; output 3. Five nodes. Middle index floor(5/2)=2 holds the value 3.

Example 2: input head = [1,2,3,4,5,6]; output 4. Six nodes. Middle index floor(6/2)=3 — the second middle — holds the value 4.

Example 3: input head = [7]; output 7. A single node is its own middle.

Category: Linked List. Topic: linked-list. Difficulty: Easy. Patterns: linked-list-traversal, fast-slow-pointers.