Problem 582
Remove Every K-th Node
Easy · 10 pts · Linked List
Problem
Delete every K-th node of a linked list: the nodes at positions K, 2K, 3K and so on, counting positions from 1 at the head. Positions are counted in the original list, before anything is deleted. Print the list that remains. If K = 1 every node is deleted and the list becomes empty. Walk the list once with a counter and unlink a node whenever the counter is a multiple of K.
Input
The first line contains N and K. The second line contains the N node values from head to tail.
Output
Print the node values of the resulting list from head to tail, separated by spaces, or EMPTY if the list has no nodes.
Constraints
1 <= N <= 100000; 1 <= K <= 100000; -10^9 <= value <= 10^9
Example input
8 3 1 2 3 4 5 6 7 8
Example output
1 2 4 5 7 8
Explanation
For 1 2 3 4 5 6 7 8 with K = 3, the nodes at positions 3 and 6 (values 3 and 6) are deleted. The remaining list is 1 2 4 5 7 8.