# Remove Nth Node From End of List Leetcode Solution

In this post, we are going to solve the Remove Nth Node From End of List Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

## Problem

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

Example 1:

```Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
```

Example 2:

```Input: head = [1], n = 1
Output: []
```

Example 3:

```Input: head = [1,2], n = 1
Output: [1]
```

Constraints:

• The number of nodes in the list is `sz`.
• `1 <= sz <= 30`
• `0 <= Node.val <= 100`
• `1 <= n <= sz`

Now, lets see the leetcode solution of Remove Nth Node From End of List Leetcode Solution.

### Remove Nth Node From End of ListLeetcode Solutionin Python

```def removeNthFromEnd(self, head, n):
fast = slow = dummy = ListNode(0)
for _ in xrange(n):
fast = fast.next
while fast and fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next```

### Remove Nth Node From End of ListLeetcode Solutionin CPP

```ListNode *removeNthFromEnd(ListNode *head, int n)
{
return nullptr;

for (int i = 0; i < n; i++)
fast = fast->next;

while (fast->next)
{
fast = fast->next;
slow = slow->next;
}

ListNode *to_de_deleted = slow->next;
slow->next = slow->next->next;

delete to_be_deleted;

}```

### Remove Nth Node From End of ListClosest Leetcode Solution in Java

```/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
while(n-- > 0) fast = fast.next;
if(fast == null){
rn = null;
}
while(fast.next != null){
slow = slow.next;
fast = fast.next;
}

ListNode rn = slow.next;
slow.next = rn.next;
rn = null;