Given the head of a linked list, remove the nth node from the end of the list and return its head.

&nbsp;
Example 1:

Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]


Example 2:

Input: head = [1], n = 1
Output: []


Example 3:

Input: head = [1,2], n = 1
Output: [1]


&nbsp;
Constraints:


	The number of nodes in the list is sz.
	1 &lt;= sz &lt;= 30
	0 &lt;= Node.val &lt;= 100
	1 &lt;= n &lt;= sz


&nbsp;
Follow up: Could you do this in one pass?

# 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]:
        if head == None:
            return None
        slow = head
        fast = head
        for i in range(n):
            fast = fast.next
        if fast == None:
            return head.next
        while fast != None and fast.next != None:
            slow = slow.next
            fast = fast.next
        slow.next = slow.next.next
        return head
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode temp = head;
        int len=0;

        if(head==null || head.next==null)
            return null;

        while(temp!=null){
            temp=temp.next;
            len++;
        }

        if(len==n)
            return head.next;

        int frontlen = len-n-1;

        ListNode first=head.next;
        ListNode second = head;

        int count=0;

        while(first!=null){
            if(count==frontlen){
                second.next=first.next;
                break;
            }else{
                first=first.next;
                second=second.next;
                count++;
            }
        }

        return head;
    }
}
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* n1 = new ListNode();
         ListNode *temp=head;
         ListNode *p=head;
        ListNode *q=n1;
        n1->next=head;
        int count;
        while(temp)
        { 
         
            if(n>0){
                n--;
            }
            else if(n==0){
            cout<val;
            p=p->next;
            q=q->next;
            }
               temp=temp->next;
        }
        q->next=p->next;
        delete p;
    return n1->next;
    }
};
var removeNthFromEnd = function(head, n) {
    let start = new ListNode(0, head);
    let slow = start, fast = start;

    for (let i =1; i <= n; i++) {
        fast = fast.next;
    }

    while(fast && fast.next) {
        slow = slow.next;
        fast = fast.next;
    }

    let nthNode = slow.next
    slow.next = nthNode.next;

    return start.next;
};
Remove Nth Node From End of List