Remove Nth Node From End

Practice this coding question on Coding Gurukul.

Problem statement

A playlist is stored as a singly linked chain. A user asks to remove the request that lies a specified number of positions from the end, while leaving all other requests in their original order. Your task Remove the kth node counted from the end of the list, with the final node counted as 1. Build a singly linked list from the input values and perform the operation on its nodes. Important details The requested node always exists. Removing the head must update the head reference, and removing the only node produces an empty list. Use two pointers separated by k positions to locate the predecessor in one traversal. Worked example For 2 -> 4 -> 6 -> 8 and k = 2, the node holding 6 is second from the end. The remaining chain is 2 -> 4 -> 8.

Input format

First line: n k. Next line: n values.

Output format

Print the resulting values in order, separated by spaces. Print EMPTY if there are no values.

Constraints

1 <= k <= n <= 30; 0 <= values <= 100.

Sample input

4 2
2 4 6 8

Sample output

2 4 8

Sign in to run or submit a solution in C++, Java or Python.