Problems › Linked lists

Remove Nth Node From End

Difficulty: Medium · Topic: Linked lists

Given a linked list as vals (head to tail) and an integer n, remove the n-th node from the end and return the remaining values. n is valid (1 ≤ n ≤ length). The classic trick is two pointers spaced n apart.

Examples

remove_nth_from_end([1,2,3,4,5], 2)
→ [1,2,3,5]

2nd from the end is 4.

remove_nth_from_end([1], 1)
→ []

Target complexity: time O(n), space O(n)

Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.

Learn the idea first: the lesson for this problem.

More Linked lists problems