Problem title
Difficulty
Avg time to solve

Search In A Grid
Moderate
15 mins
Word Pattern
Easy
15 mins
Make Palindrome
Easy
--
Gas Stations
Moderate
10 mins
Number of GP sequence
Moderate
10 mins
Perfect Number
Easy
10 mins
Form the Biggest Number
Moderate
25 mins
Check Square
Moderate
10 mins
Sorted Subsequence of Size 3
Moderate
15 mins
Remove maximum edges
Easy
15 mins
2

Delete N nodes after M nodes of a linked list

Difficulty: MEDIUM
Contributed By
Avg. time to solve
30 min
Success Rate
65%

Problem Statement

You have given a singly linked list and two integers 'N' and 'M'. Delete 'N' nodes after every 'M' node, or we can say more clearly that traverse the linked list such that you retain 'M' nodes then delete next 'N' nodes, continue the same till the end of the linked list.

Follow Up:
Try to solve this problem in O(N) time complexity and O(1) space complexity.
Input format :
The first line of input contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.

The second line contains the two single space-separated integers 'N' and 'M'.
Output format :
For each test case, print a single line containing space-separated integers denoting resultant linked list after performing required operation denoted as the elements separated by a single space and terminated by -1.

The output of each test case will be printed in a separate line.

Note:

You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints :
0 <= L <= 10 ^ 6
-10 ^ 9 <= DATA <= 10 ^ 9 and DATA != -1
0 <= N <= 10 ^ 6
1 <= M <= 10 ^ 6

Where 'L' is the number of nodes in Linked List and 'N' and 'M' are parameters of the Problem and 'DATA' is the value of any node.

Time Limit: 1 sec.
Sample Input 1 :
3 2 0 -4 -1
1 1
Sample Output 1 :
3 0 -1
Explanation to Sample Input 1 :
The first and third Nodes are retained, and rest are deleted.
Sample Input 2 :
3 2 0 -4 7 -9 -8 10 5 -1
1 2
Sample Output 2 :
3 2 -4 7 -8 10 -1
Explanation to Sample Input 2 :
The first, second, fourth, fifth, seventh and eighth Nodes are retained, and rest are deleted.
Sample Input 3 :
3 2 0 -4 7 -9 -8 10 5 -1
2 1
Sample Output 3 :
3 -4 -8 -1
Explanation to Sample Input 3 :
The first, fourth and seventh Nodes are retained, and rest are deleted.
Reset Code
Full screen
copy-code
Console