Description
Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes (even number of nodes), return the second middle node.
Examples
Input:
head = [1,2,3,4,5]Output:
3Explanation:
The middle node is 3.
Input:
head = [10,20]Output:
20Explanation:
With only 2 nodes, there are two potential middle positions. According to the problem rule, when there are two middle nodes, the second one is returned, which is 20.
Input:
head = [7,14,21,28,35,42,49]Output:
28Explanation:
This list has 7 nodes. The middle position is at index 3 (counting from 0), which contains the value 28. With an odd number of nodes, there's exactly one middle node.
Constraints
- •
1 ≤ list length ≤ 100