Top 150 InterviewEasy

Remove Nth Node From End of List

Detailed guide and Python implementation for the 'Remove Nth Node From End of List' problem.

Problem Statement

Easy

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

The linked list is represented as a Python list. Implement a function removeNthFromEnd(head: list, n: int) -> list that returns the list after removal.

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

Examples

Example 1
Input
[1,2,3,4,5], 2
Output
[1,2,3,5]
Explanation

The 2nd node from the end is 4. After removing it, the list becomes 1->2->3->5.

Example 2
Input
[1], 1
Output
[]
Explanation

There is only one node and we remove it, so the list becomes empty.

Example 3
Input
[1,2], 1
Output
[1]
Explanation

The 1st node from the end is 2. After removing it, the list becomes [1].

Need a Hint?
Consider using Linked List-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.