```
class Solution {
private:
int getLength(ListNode *head) {
int length = 0;
while(head) {
head = head->next;
length++;
}
return length;
}
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
if(n<1) return head;
ListNode tempHead(0);
ListNode* headptr = &tempHead;
headptr->next = head;
int index = getLength(head) - n;
for(int i = 0; i < index; i++)
headptr = headptr->next;
ListNode* temp = headptr->next;
headptr->next = headptr->next->next;
delete temp;
return tempHead.next;
}
};
```