Back to Practice Dashboard
Top 150 InterviewEasy

Remove Nth Node From End of List

Learn how to solve the 'Remove Nth Node From End of List' problem. This detailed resource details brute force and optimized approaches.

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?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor