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.
head = [1,2,3,4,5][3,4,5]head = [1,2,3,4,5,6][4,5,6]slow = head and fast = head at index 0fast != null && fast.next != null: loop continuesslow = slow.next (1 step) and fast = fast.next.next (2 steps)fast at null or last node. slow is now at the middle. Return slow.Since fast moves exactly twice as fast as slow, when fast has covered the entire list (reaches null or the last node), slow has covered exactly half the distance — landing on the middle. For even-length lists, fast lands on the last node after covering n−1 steps, so slow ends at index ⌊n/2⌋ (the second middle), matching LeetCode's requirement.
Counting the length first and then walking half of it takes two passes. One pass is enough with two speeds.
Move slow by one and fast by two. When fast reaches the end, slow has covered half the distance. So slow is at the middle.
When fast finishes, slow is halfway. This also finds the split point for palindrome and reorder problems.
Run the animation above on your own input, or watch the 0:59 video explanation of Middle of Linked List with a dry run and Java code.