Given the head of a singly linked list and an integer n, remove the nth node from the end of the list and return the head of the modified list. The function should handle all valid cases, including removing the head node.
Example 1:
Input: head = [1, 2, 3, 4, 5], n = 2
Output: [1, 2, 3, 5]
Explanation: The 2nd node from the end is 4, so it is removed.
Example 2:
Input: head = [1], n = 1
Output: []
Explanation: The only node is removed, leaving an empty list.
1 <= number of nodes <= 10^51 <= n <= number of nodes-10^4 <= node.val <= 10^4