Please solve the programming task below using a self-contained code snippet in a markdown code block.

Given the head of a linked list, remove the nth node from the end of the list and return its head.
 
Please complete the following python code precisely:
```python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
```