```
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if((n ==1) && (head.next==null))
return null;
int length = 0;
ListNode currentNode = head;
while(currentNode != null)
{
currentNode=currentNode.next;
length++;
}
currentNode=head;
if(n == 1)
{
for(int i=0;i<length-2;i++)
{
currentNode = currentNode.next;
}
currentNode.next = null;
return head;
}
if(n==length)
{
head = head.next;
return head;
}
for(int i=0;i<length-n-1;i++)
{
currentNode=currentNode.next;
}
ListNode latter = currentNode.next.next;
currentNode.next = latter;
return head;
}
```

}