# Leetcode 19. Remove Nth Node From End of List

## 1. Description

Remove Nth Node From End of List

## 2. Solution

• Version 1
``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
return nullptr;
}
int number = 0;
while(current) {
if(current->next) {
number += 2;
current = current->next->next;
}
else {
number += 1;
current = current->next;
}
}
int target = number - n;
if(target == 0) {
}
int count = 1;
while(count != target) {
pre = current;
current = current->next;
count++;
}
pre->next = pre->next->next;
}
};
``````
• Version 2
``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
return nullptr;
}
int number = 0;
int count = 0;
while(current) {
count++;
middle = middle->next;
if(current->next) {
number += 2;
current = current->next->next;
}
else {
number += 1;
current = current->next;
}
}
int target = number - n;
if(target == 0) {
}
ListNode* pre = nullptr;
int index = 0;
if(target > count) {
index = count;
current = middle;
}
else {
}
while(index != target) {
pre = current;
current = current->next;
index++;
}
pre->next = pre->next->next;
}
};
``````
• Version 3
``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
for(int i = 0; i < n + 1; i++) {
current = current->next;
}