Remove Nth Node From End

Linked Lists · medium

A linked list is given as an array of values in order (e.g. `[1,2,3,4,5]`). Implement `removeNthFromEnd(arr, n)` that removes the **n-th node from the end** of the list and returns the resulting array. `n` is always valid (1 ≤ n ≤ arr.length). Example: `removeNthFromEnd([1,2,3,4,5], 2)` → `[1,2,3,5]` (the 2nd from end, which is `4`, is removed).

Hints
  • The n-th from end has index `length - n` in the array.
  • Use two pointers separated by n steps to find this position in a single pass over a real linked list. Here you can compute it directly from the array length.

Learning resources

Loading...

Run your code to see results.