head = [1,2,3,4,5,6]node 4 → [4,5,6]Find the middle node of a nonempty singly linked list. If its length is even, choose the second middle node. Return the original node with its remaining chain; leave all values and links unchanged.
head = [1,2,3,4,5,6]node 4 → [4,5,6]Array notation shows node values. The editor supplies ListNode with val and next. Your function receives a head node and must return a node, not an array or a copied chain.
LeetCode 876head = [1,2,3,4,5,6] result = node 4 → [4,5,6]
Counting visits n nodes; the second pass follows floor(n / 2) links. Their sum is O(n), not O(n²).
A counter and a fixed number of node references use O(1) extra space.
head = [1,2,3,4,5,6] result = node 4 → [4,5,6]
Both approaches already use O(n) time and O(1) extra space. Slow and fast pointers avoid counting and restarting, finding the middle in one pass.
The loop runs floor(n / 2) times, with constant work each time: O(n) time.
Only slow and fast references are needed. No array, recursion stack, or replacement nodes: O(1) extra space.