```
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int count = 1;
ListNode* curr = head;
while(curr->next != NULL) {
count++;
curr = curr->next;
}
curr = head;
if(count != n) {
for(int i = 0; i < count - n - 1; i++) {
curr = curr->next;
}
curr->next = curr->next->next;
return head;
} else return head->next;
}
};
```