# Remove Nth Node From End of List

## Problem

### Description

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

#### Notice

The minimum number of nodes in list is n.

#### Example

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

After removing the second node from the end, the linked list becomes 1->2->3->5->null.

#### Challenge

Can you do it without getting the length of the linked list?

## 题解

### C++

/**
* Definition of ListNode
* class ListNode {
* public:
*     int val;
*     ListNode *next;
*     ListNode(int val) {
*         this->val = val;
*         this->next = NULL;
*     }
* }
*/
class Solution {
public:
/**
* @param n: An integer.
*/
ListNode *removeNthFromEnd(ListNode *head, int n) {
if (NULL == head || n < 1) {
}

ListNode dummy(0);
ListNode *preDel = dummy;

for (int i = 0; i != n; ++i) {
return NULL;
}
}

preDel = preDel->next;
}
preDel->next = preDel->next->next;

return dummy.next;
}
};


### Java

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {

ListNode dummy = new ListNode(0);
ListNode slow = dummy;
for (int i = 0; i < n; i++) {
fast = fast.next;
}

while(fast != null) {
fast = fast.next;
slow = slow.next;
}

// gc friendly
// ListNode toBeDeleted = slow.next;
slow.next = slow.next.next;
// toBeDeleted.next = null;
// toBeDeleted = null;

return dummy.next;
}
}