Given a linked list and two integers m and n, the task is to traverse the linked list such that you skip m nodes, then delete the next n nodes, and continue the same till end of the linked list.
Note: m cannot be 0.
Example:
Input: Linked List: 9->1->3->5->9->4->10->1, n = 1, m = 2
Output: 9->1->5->9->10->1
Explanation: Deleting 1 node after skipping 2 nodes each time, we have list as 9-> 1-> 5-> 9-> 10-> 1.
Input: Linked List: 1->2->3->4->5->6, n = 1, m = 6
Output: 1->2->3->4->5->6
Explanation: After skipping 6 nodes for the first time , we will reach of end of the linked list, so, we will get the given linked list itself.
The idea is to go through a linked list and, for every set of nodes, keep the first m nodes and delete the next n nodes, and then repeat this process until the end of the list.
Step-by-step approach:
Start with a pointer at the first node (the head) of the linked list.
Move this pointer forward m times, so it skips over m nodes.
After skipping m nodes, the next n nodes should be deleted.
To do this, we disconnect these n nodes from the list and free their memory.
After deleting the n nodes, repeat the process starting from the next node. Skip the next m nodes and delete the following n nodes.
This process will stops when we reach to end of the list, either because there are fewer than m nodes left to skip or there are no nodes left to delete.
Below is the implementation of the above approach:
C++
// C++ program to delete n nodes// after m nodes of a linked list #include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function to skip m nodes and then// delete n nodes of the linked list. Node*skipMdeleteN(Node*head,intm,intn){Node*curr=head,*t;// The main loop that traverses// through the whole list while(curr!=nullptr){// Skip m nodes for(inti=1;i<m&&curr!=nullptr;i++)curr=curr->next;// If we reached end of list, then return if(curr==nullptr)returnhead;// Start from next node and delete n nodes t=curr->next;for(inti=1;i<=n&&t!=nullptr;i++){Node*temp=t;t=t->next;delete(temp);}// Link the previous list with// remaining nodes curr->next=t;// Set current pointer for next iteration curr=t;}returnhead;}voidprintList(Node*head){Node*curr=head;while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;}intmain(){// Create following linked list:// 1->2->3->4->5->6Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);head->next->next->next->next->next=newNode(6);intm=2,n=2;head=skipMdeleteN(head,m,n);printList(head);return0;}
C
// C program to delete n nodes after// m nodes of a linked list#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Function to skip m nodes and then delete n nodes of the linked liststructNode*skipMdeleteN(structNode*head,intm,intn){structNode*curr=head;// Current node pointerstructNode*t;// Temporary node pointerintcount;// Counter variable// Traverse through the entire linked listwhile(curr!=NULL){// Skip m nodesfor(count=1;count<m&&curr!=NULL;count++)curr=curr->next;// If end of the list is reached, return the headif(curr==NULL)returnhead;// Start from the next node and delete n nodest=curr->next;for(count=1;count<=n&&t!=NULL;count++){structNode*temp=t;t=t->next;free(temp);}// Link the current node to the remaining listcurr->next=t;// Move the current pointer to the next nodecurr=t;}returnhead;}voidprintList(structNode*head){structNode*curr=head;while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}printf("\n");}structNode*createNode(intdata){structNode*temp=(structNode*)malloc(sizeof(structNode));temp->data=data;temp->next=NULL;returntemp;}intmain(){// Create the following linked list: 1->2->3->4->5->6structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(3);head->next->next->next=createNode(4);head->next->next->next->next=createNode(5);head->next->next->next->next->next=createNode(6);intm=2,n=2;head=skipMdeleteN(head,m,n);printList(head);return0;}
Java
// Java program to delete n nodes// after m nodes of a linked listclassNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}classGfG{// Function to skip m nodes and then delete n nodes// of the linked liststaticNodeskipMdeleteN(Nodehead,intm,intn){Nodecurr=head;// Current node pointerNodet;// Temporary node pointerintcount;// Counter variable// Traverse through the entire linked listwhile(curr!=null){// Skip m nodesfor(count=1;count<m&&curr!=null;count++)curr=curr.next;// If end of the list is reached, return the// headif(curr==null)returnhead;// Start from the next node and delete n nodest=curr.next;for(count=1;count<=n&&t!=null;count++){Nodetemp=t;t=t.next;// Dereference the node for garbage// collectiontemp=null;}// Link the current node to the remaining listcurr.next=t;// Move the current pointer to the next nodecurr=t;}returnhead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){// Create the following linked list:// 1->2->3->4->5->6Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);intm=2,n=2;head=skipMdeleteN(head,m,n);printList(head);}}
Python
# Python program to delete n nodes# after m nodes of a linked listclassNode:def__init__(self,data):self.data=dataself.next=None# Function to skip m nodes and then delete n nodes# of the linked listdefskipMdeleteN(head,m,n):curr=head# Current node pointercount=0# Counter variable# Traverse through the entire linked listwhilecurr:# Skip m nodesforcountinrange(1,m):ifcurrisNone:returnheadcurr=curr.next# If end of the list is reached, return the headifcurrisNone:returnhead# Start from the next node and delete n nodest=curr.nextforcountinrange(1,n+1):iftisNone:breaktemp=tt=t.nextdeltemp# Delete the node# Link the current node to the remaining listcurr.next=t# Move the current pointer to the next nodecurr=treturnheaddefprintList(head):curr=headwhilecurr:print(curr.data,end=" ")curr=curr.nextprint()if__name__=="__main__":# Create the following linked list: 1->2->3->4->5->6head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)head.next.next.next.next.next=Node(6)m=2n=2head=skipMdeleteN(head,m,n)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Function to skip m nodes and then delete n nodes of// the linked listpublicstaticNodeskipMdeleteN(Nodehead,intm,intn){Nodecurr=head;// Current node pointerNodet;// Temporary node pointerintcount;// Counter variable// Traverse through the entire linked listwhile(curr!=null){// Skip m nodesfor(count=1;count<m&&curr!=null;count++)curr=curr.next;// If end of the list is reached, return the// headif(curr==null)returnhead;// Start from the next node and delete n nodest=curr.next;for(count=1;count<=n&&t!=null;count++){t=t.next;// Move to the next node}// Link the current node to the remaining listcurr.next=t;// Move the current pointer to the next nodecurr=t;}returnhead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(){// Create the following linked list:// 1->2->3->4->5->6Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);intm=2,n=2;head=skipMdeleteN(head,m,n);printList(head);}}
JavaScript
// Javascript program to delete n nodes// after m nodes of a linked listclassNode{constructor(data){this.data=data;this.next=null;}}// Function to skip m nodes and then delete n nodes of the// linked listfunctionskipMdeleteN(head,m,n){letcurr=head;// Current node pointerletcount=0;// Counter variable// Traverse through the entire linked listwhile(curr!==null){// Skip m nodesfor(count=1;count<m&&curr!==null;count++)curr=curr.next;// If end of the list is reached, return the headif(curr===null)returnhead;// Start from the next node and delete n nodeslett=curr.next;for(count=1;count<=n&&t!==null;count++){lettemp=t;t=t.next;// Dereference the nodetemp=null;}// Link the current node to the remaining listcurr.next=t;// Move the current pointer to the next nodecurr=t;}returnhead;}functionprintList(head){letcurr=head;while(curr!==null){process.stdout.write(curr.data+" ");curr=curr.next;}console.log();}// Create the following linked list: 1->2->3->4->5->6lethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);letm=2,n=2;// Perform the skip and delete operationhead=skipMdeleteN(head,m,n);// Print the modified linked listprintList(head);
Output
1 2 5 6
Time Complexity: O(n), where n is the total number of nodes in the linked list. This is because the algorithm processes each node exactly once, either by skipping it or by deleting it. Auxiliary Space: O(1)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.