/**
* 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) {
if (head == nullptr || n == 0)
{
return nullptr;
}
ListNode* f = head;
ListNode* s = head;
int num = 0;
ListNode* go = head;
while(go != nullptr){
go = go->next;
num++;
}
if(num == n){
return head->next;
}
while(n > 0 && f->next !=nullptr){
f = f->next;
n--;
}
while(f->next != nullptr){
f = f->next;
s = s->next;
}
ListNode* help = s->next;
s->next = help->next;
delete help;
return head;
}
};