Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
Explanation: The middle node of the list is node 3.
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
[1, 100].1 <= Node.val <= 100To find the middle of a linked list, one straightforward approach is to count the total number of nodes in the list first, and then iterate through the list again to find the node at the n/2 position, where n is the total number of nodes.
n/2 using integer division for even numbers (the middle is the second middle node).The two-pointer technique is often very efficient for linked list problems. We can use two pointers, slow and fast, to identify the middle of the list. The fast pointer moves twice as fast as the slow pointer. By the time the fast pointer reaches the end of the list, the slow pointer will be at the middle.
slow and fast, both starting at the head of the list.slow pointer by one node and the fast pointer by two nodes in each iteration.fast reaches the end of the list or the node just before the end (in case of even length).slow pointer will be at the middle of the list when the loop terminates.slow pointer.