304 North Cardinal St.
Dorchester Center, MA 02124

# Remove Nth Node From End of List LeetCode Solution

## Problem – Remove Nth Node From End of List

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

### Remove Nth Node From End of List LeetCode Solution in Python

class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
for _ in range(n): fast = fast.next
while fast.next: fast, slow = fast.next, slow.next
slow.next = slow.next.next

### Remove Nth Node From End of List LeetCode Solution in Java

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
for (int i = 0; i < n; i++) fast = fast.next;
if (fast == null) return head.next;
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
}
}

### Remove Nth Node From End of List LeetCode Solution in C++

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
for (int i = 0; i < n; i++) fast = fast->next;
while (fast->next) fast = fast->next, slow = slow->next;
slow->next = slow->next->next;
}
};
##### Remove Nth Node From End of List LeetCode Solution Review:

In our experience, we suggest you solve this Remove Nth Node From End of List LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Remove Nth Node From End of List LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Remove Nth Node From End of List LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions