LeetCode 876 — Middle of the Linked List. Return the middle node of a singly linked list. If there are two middles, return the second. We solve it in a single pass with O(1) space using the fast-slow pointer pattern.
What you'll learn:
→ Why 2× speed means slow covers exactly half the distance
→ How one while condition handles both odd and even length lists differently
→ The two exit cases: fast.next = null (odd) vs fast = null (even)
→ Step-by-step trace for [1,2,3,4,5] and [1,2,3,4,5,6]
→ Clean 4-line Python implementation — no helper, no length calculation
Timestamps:
0:00 Problem — odd vs even middle
0:23 Why 2× speed lands slow at the middle
0:53 Loop condition — two exit cases explained
1:22 Trace: [1,2,3,4,5] (odd length)
1:40 Trace: [1,2,3,4,5,6] (even length)
2:03 Python code + complexity
Code:
def middle_node(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
Time: O(n) — one pass | Space: O(1) — two pointers
Part of the Fast and Slow Pointers series — subscribe for a new algorithm every week.
#LeetCode #FastAndSlowPointers #Algorithms #Python #CodingInterview #DataStructures #LinkedList #MiddleNode
```
Chapters
```
0:00 Problem — odd and even length examples
0:23 Why 2× speed = middle position
0:53 Loop condition: two exit cases
1:22 Trace: [1,2,3,4,5] (odd)
1:40 Trace: [1,2,3,4,5,6] (even)
2:03 Python code + complexity
```
Tags
```
leetcode, middle of linked list, leetcode 876, fast and slow pointers, tortoise and hare, two pointers, linked list, python, coding interview, data structures, algorithms, neetcode, blind 75, single pass, O(1) space