# Remove Nth Node From End Of The List Program Solution

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:

Problem Statement:

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`

Follow up: Could you do this in one pass?

• Time: O(n)O(n)
• Space: O(1)O(1)

### Remove Nth Node From End Of The List Program Solution In C++

``````class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {

while (n--)
fast = fast->next;
if (!fast)

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

}
};
``````

### Remove Nth Node From End Of The List Program Solution In JAVA

``````class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {

while (n-- > 0)
fast = fast.next;
if (fast == null)

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

}
}
``````

### Remove Nth Node From End Of The List Program Solution In PYTHON

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

for _ in range(n):
fast = fast.next
if not fast:
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}