Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.
Difficulty Level: Rookie
Examples:
Input:
M = 2, N = 2
Linked List: 1->2->3->4->5->6->7->8
Output:
Linked List: 1->2->5->6
Input:
M = 3, N = 2
Linked List: 1->2->3->4->5->6->7->8->9->10
Output:
Linked List: 1->2->3->6->7->8
Input:
M = 1, N = 1
Linked List: 1->2->3->4->5->6->7->8->9->10
Output:
Linked List: 1->3->5->7->9
The main part of the problem is to maintain proper links between nodes, and make sure that all corner cases are handled. Following is C implementation of function skipMdeleteN() that skips M nodes and delete N nodes till end of list. It is assumed that M cannot be 0.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node *next;
};
void push(Node ** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout<<temp->data<< " " ;
temp = temp->next;
}
cout<<endl;
}
void skipMdeleteN(Node *head, int M, int N)
{
Node *curr = head, *t;
int count;
while (curr)
{
for (count = 1; count < M &&
curr!= NULL; count++)
curr = curr->next;
if (curr == NULL)
return ;
t = curr->next;
for (count = 1; count<=N && t!= NULL; count++)
{
Node *temp = t;
t = t->next;
free (temp);
}
curr->next = t;
curr = t;
}
}
int main()
{
Node* head = NULL;
int M=2, N=3;
push(&head, 10);
push(&head, 9);
push(&head, 8);
push(&head, 7);
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
cout << "M = " << M<< " N = " << N << "\nGiven Linked list is :\n" ;
printList(head);
skipMdeleteN(head, M, N);
cout<< "\nLinked list after deletion is :\n" ;
printList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void push( struct Node ** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf ( "%d " , temp->data);
temp = temp->next;
}
printf ( "\n" );
}
void skipMdeleteN( struct Node *head, int M, int N)
{
struct Node *curr = head, *t;
int count;
while (curr)
{
for (count = 1; count<M && curr!= NULL; count++)
curr = curr->next;
if (curr == NULL)
return ;
t = curr->next;
for (count = 1; count<=N && t!= NULL; count++)
{
struct Node *temp = t;
t = t->next;
free (temp);
}
curr->next = t;
curr = t;
}
}
int main()
{
struct Node* head = NULL;
int M=2, N=3;
push(&head, 10);
push(&head, 9);
push(&head, 8);
push(&head, 7);
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printf ( "M = %d, N = %d \nGiven Linked list is :\n" , M, N);
printList(head);
skipMdeleteN(head, M, N);
printf ( "\nLinked list after deletion is :\n" );
printList(head);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList( Node head)
{
Node temp = head;
while (temp != null )
{
System.out.printf( "%d " , temp.data);
temp = temp.next;
}
System.out.printf( "\n" );
}
static void skipMdeleteN( Node head, int M, int N)
{
Node curr = head, t;
int count;
while (curr!= null )
{
for (count = 1 ; count < M && curr != null ; count++)
curr = curr.next;
if (curr == null )
return ;
t = curr.next;
for (count = 1 ; count <= N && t != null ; count++)
{
Node temp = t;
t = t.next;
}
curr.next = t;
curr = t;
}
}
public static void main(String args[])
{
Node head = null ;
int M= 2 , N= 3 ;
head=push(head, 10 );
head=push(head, 9 );
head=push(head, 8 );
head=push(head, 7 );
head=push(head, 6 );
head=push(head, 5 );
head=push(head, 4 );
head=push(head, 3 );
head=push(head, 2 );
head=push(head, 1 );
System.out.printf( "M = %d, N = %d \nGiven" +
"Linked list is :\n" , M, N);
printList(head);
skipMdeleteN(head, M, N);
System.out.printf( "\nLinked list after deletion is :\n" );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while (temp):
print (temp.data,end = " " )
temp = temp. next
def skipMdeleteN( self , M, N):
curr = self .head
while (curr):
for count in range ( 1 , M):
if curr is None :
return
curr = curr. next
if curr is None :
return
t = curr. next
for count in range ( 1 , N + 1 ):
if t is None :
break
t = t. next
curr. next = t
curr = t
llist = LinkedList()
M = 2
N = 3
llist.push( 10 )
llist.push( 9 )
llist.push( 8 )
llist.push( 7 )
llist.push( 6 )
llist.push( 5 )
llist.push( 4 )
llist.push( 3 )
llist.push( 2 )
llist.push( 1 )
print ( "M = %d, N = %d\nGiven Linked List is:" % (M, N))
llist.printList()
print ()
llist.skipMdeleteN(M, N)
print ( "\nLinked list after deletion is" )
llist.printList()
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList( Node head)
{
Node temp = head;
while (temp != null )
{
Console.Write( "{0} " , temp.data);
temp = temp.next;
}
Console.Write( "\n" );
}
static void skipMdeleteN( Node head, int M, int N)
{
Node curr = head, t;
int count;
while (curr!= null )
{
for (count = 1; count < M &&
curr != null ; count++)
curr = curr.next;
if (curr == null )
return ;
t = curr.next;
for (count = 1; count <= N && t != null ; count++)
{
Node temp = t;
t = t.next;
}
curr.next = t;
curr = t;
}
}
public static void Main(String []args)
{
Node head = null ;
int M=2, N=3;
head=push(head, 10);
head=push(head, 9);
head=push(head, 8);
head=push(head, 7);
head=push(head, 6);
head=push(head, 5);
head=push(head, 4);
head=push(head, 3);
head=push(head, 2);
head=push(head, 1);
Console.Write( "M = {0}, N = {1} \nGiven" +
"Linked list is :\n" , M, N);
printList(head);
skipMdeleteN(head, M, N);
Console.Write( "\nLinked list after deletion is :\n" );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function printList(head) {
var temp = head;
while (temp != null ) {
document.write(temp.data+ " " );
temp = temp.next;
}
document.write( "<br/>" );
}
function skipMdeleteN(head , M , N) {
var curr = head, t;
var count;
while (curr != null ) {
for (count = 1; count < M && curr != null ;
count++)
curr = curr.next;
if (curr == null )
return ;
t = curr.next;
for (count = 1; count <= N && t != null ;
count++)
{
var temp = t;
t = t.next;
}
curr.next = t;
curr = t;
}
}
var head = null ;
var M = 2, N = 3;
head = push(head, 10);
head = push(head, 9);
head = push(head, 8);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
document.write(
"M = " +M+ ", N = " +N+ "<br/>Given Linked list is :<br/>"
);
printList(head);
skipMdeleteN(head, M, N);
document.write(
"<br/>Linked list after deletion is :<br/>"
);
printList(head);
</script>
|
Output
M = 2 N = 3
Given Linked list is :
1 2 3 4 5 6 7 8 9 10
Linked list after deletion is :
1 2 6 7
Time Complexity: O(n) where n is number of nodes in linked list.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Jan, 2023
Like Article
Save Article