PIXELBANKv8.2.1
Menu

Remove Nth Node From End of List

Given a list of values and an integer n, remove the n-th node from the end of the list and return the result as space-separated values.

Example:

Input:
1,2,3,4,5
2
Output:
1 2 3 5
Reasoning:
  • The input list is 1,2,3,4,51, 2, 3, 4, 5 and n=2n = 2, meaning we need to remove the 2nd node from the end.
  • To find the node to remove, we calculate its position from the start: 52+1=45 - 2 + 1 = 4, so the 4th node from the start needs to be removed.
  • The node at the 4th position has a value of 44, which is removed from the list.
  • The resulting list is 1,2,3,51, 2, 3, 5, which is returned as space-separated values: 12351 2 3 5.

Constraints:

  • 1 <= number of nodes <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= number of nodes
Editor

Test Results

0/0
Run code to see test results.