You are given the head of a singly linked list. Your task is to find and return the middle node of the linked list. If the linked list contains an even number of nodes, return the second middle node.
Example 1
Input: head = [1,2,3,4,5]
Output: 3
Explanation: The linked list contains 5 nodes. The middle node is 3.
Example 2
Input: head = [1,2,3,4,5,6]
Output: 4
Explanation: The linked list contains 6 nodes. There are two middle nodes (3 and 4), so the second middle node, 4, is returned.
Example 3
Input: head = [10]
Output: 10
Explanation: The linked list contains only one node, so it is the middle node.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 5
3
1 2 3 4 5 6
4