# LeetCode 19. Remove Nth Node From End of List

## 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?

## Explanation

We introduce a preDelete pointer first and place it at the beginning of the linked list.

1. Move head pointer first so that the distance between preDelete and head pointer would be N nodes.
2. Move head and preDelete pointer together until head is reaching to the end.
3. Modify preDelete pointing node.next to the next node of the delete node.

## Java Solution

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

ListNode dummy = new ListNode(0);

ListNode preDelete = dummy;

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

preDelete = preDelete.next;
}

preDelete.next = preDelete.next.next;

return dummy.next;
}
}```

## Python Solution

``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:

for i in range(0, n):
fast = fast.next
if fast == None:

while fast.next != None:
fast = fast.next
slow = slow.next

slow.next = slow.next.next

1. Gaurav Kawatra says:
1. sanjeev says: