Remove Nth Node From End of List
the most important thing is that you must know the length of the list;
l is length;
then remove is easy
And the complete code
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int l = 0;
ListNode* beg = new ListNode(0); // you should create a new ListNode
beg->next = head;
ListNode* first = head;
while(first)
{
l++;
first = first->next;
}
l = l - n;
first = beg;
while(l>0)
{
l--;
first = first->next;
}
first->next = (first->next)->next;
return beg->next;
}
};