# Remove Nth Node From End of List Problem Solution

Remove Nth Node From End of List Problem: Given the head of a linked list, remove the nth node from the end of the list and return its head.

Example :

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

## Problem Solution In Python

class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:

for i in range(n):
fast = fast.next

if not fast:

while fast and fast.next:
slow = slow.next
fast = fast.next

slow.next = slow.next.next

## Problem Solution In Java

public ListNode removeNthFromEnd(ListNode head, int n) {
return null;

for(int i = 1; i < n; i++)
fast = fast.next;
while(fast.next != null) {
fast = fast.next;
prev = slow;
slow = slow.next;
}
prev.next = slow.next;
}

## Problem Solution In C++

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {

//checking basic  conditions

ListNode* prev = NULL;

while(n!=0){
fast = fast->next;
n--;
}

while(fast!=NULL){
prev = slow;
fast=fast->next;
slow=slow->next;
}

if(prev==NULL){
delete slow;
}

prev->next = slow->next;
delete slow;
}
};

## Problem Solution In C

struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
struct ListNode* current;
int count = 0;

while(current != NULL)
{
current = current->next;
count++;
}

if(count==n)
return current->next;

count = count-n-1;
while(count--)
{
current = current->next;
}

current->next = current->next->next;