Middle of the Linked List
Given the head of a singly linked list, return the middle node. If there are two middle nodes (even length), return the second of the two.
Open official problem prompt ↗Return the middle node of a singly linked list in one pass, preferring the second middle when the length is even.
Two runners on a track starting together; the faster runs twice as fast, so when they reach the finish the slower runner is exactly at the halfway marker.
- Input
- head = [1,2,3,4,5]
- Output
- [3,4,5]
- Why
- The list has 5 nodes, so the middle is the 3rd node (value 3); returning it yields the sublist 3 -> 4 -> 5.
The number of nodes is in the range [1, 100]1 <= Node.val <= 100