# Remove Nth Node From End of List

Oct 07, 2017

Given a linked list, remove the nth node from the end of list and return its head.

For example, Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one pass.

int getRightN(ListNode *head,int n) {
int size = 0;
while(p != nullptr) {
p = p->next;
size++;
}
return size - n;
}
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode h(0);
ListNode *p = &h;

int k = getRightN(p,n);
while(--k)
p=p->next;

return h.next;
}


ListNode *removeNthFromEnd(ListNode *head, int n)
{
return nullptr;

for (int i = 0; i < n; i++)
fast = fast->next;

while (fast->next)
{
fast = fast->next;
slow = slow->next;
}

ListNode *to_de_deleted = slow->next;
slow->next = slow->next->next;

delete to_be_deleted;

}


ListNode* removeNthFromEnd(ListNode* head, int n)
{
for(int i = 1; i < n; ++i)
{
t2 = t2->next;
}
while(t2->next != NULL)
{
t1 = &((*t1)->next);
t2 = t2->next;
}
*t1 = (*t1)->next;