📘
Remove Nth Node From End of List
MediumLinked 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,5 and n=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: 5−2+1=4, so the 4th node from the start needs to be removed.
- The node at the 4th position has a value of 4, which is removed from the list.
- The resulting list is 1,2,3,5, which is returned as space-separated values: 1235.
Constraints:
- 1 <= number of nodes <= 30
- 0 <= Node.val <= 100
- 1 <= n <= number of nodes
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.