Intermediate Level

Implement the removeNthNodeFromEnd method that removes the nth node from the end and returns the updated list values.

The linked list is provided as an integer array nodes, its length size, and an integer n.

Your task is to remove the nth node from the end of the linked list and return the remaining values in order.

For example, in [1,2,3,4,5] with n = 2, the second node from the end is 4, so the result is [1,2,3,5].

Example 1
Input:
nodes (int[]) = [1,2,3,4,5]
size (int) = 5
n (int) = 2
Return:
(int[]) [1,2,3,5]
Example 2
Input:
nodes (int[]) = [1]
size (int) = 1
n (int) = 1
Return:
(int[]) []
Example 3
Input:
nodes (int[]) = [1,2]
size (int) = 2
n (int) = 1
Return:
(int[]) [1]

In a real linked list, this can be solved using two pointers separated by n steps. When the first pointer reaches the end, the second pointer is just before the node to remove.

Because the linked list is represented as an array here, the index to remove is size - n. Copy all values except this index into the result array.

Pseudocode:

function removeNthNodeFromEnd(nodes, size, n):
    removeIndex = size - n
    result = empty array
    for i from 0 to size - 1:
        if i != removeIndex:
            append nodes[i] to result
    return result
Run your code to see the result.