Problem: Remove Nth Node From End of List
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Example 1:
Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
```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;
}
} ```