# 19. Remove Nth Node From End of List

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?

# My Solution(92ms 22.5MB)

``````/**
* public class ListNode {
*     public int val;
*     public ListNode next;
*     public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode ret = new ListNode(0);
int length = 0;
while(temp != null)
{
temp = temp.next;
length += 1;
}
length -= n;
if (length == 0)
{
{
return null;
}
else
{
}
}
else
{
for(int i = 0; i < length - 1; i++)
{
temp = temp.next;
}
temp.next = temp.next.next;
}
return ret.next;
}
}``````

# No.1 Solution(88ms)

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

ListNode preDeleted;
ListNode dummy=new ListNode(0);
preDeleted=dummy;

for(int index=0;index<n;index++)
{
return null;

}

{
preDeleted=preDeleted.next;