## Description

``Given a linked list, remove the n-th node from the end of list and return its head.``

## 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.``

``Could you do this in one pass?``

## Solution 1(C++)

``````class Solution{
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int listlen = 0;
while(node != nullptr){
listlen++;
node = node->next;
}

ListNode* pre = nullptr;
for(int i=0; i<listlen-n; i++){
pre = target;
target = target->next;
}
pre->next = target->next;
}
};``````

## Solution 2(C++)

``````class Solution{
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* top = new ListNode(-1);
ListNode* front = top;
ListNode* behind = top;
ListNode* pre = nullptr;
for(int i=0; i<n; i++) front = front->next;
while(front != nullptr){
front = front->next;
pre = behind;
behind = behind->next;
}
pre->next = behind->next;
}
};``````