×

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list

In this problem, we have given a linked list and two integers M and N. We need to traverse the linked list in such a way that we retain M nodes of the linked list and delete N nodes of the linked list continually till the end of the linked list.

Examples:

Input:

M = 3, N = 2

Linked List: 2 ->4 ->6 ->8 ->10 ->12 ->14 ->16

Output:

Linked List: 2 -> 4 -> 6 -> 12 -> 14 -> 16

Input:

M = 1, N = 3

Linked List: 0 -> 12 -> 13 ->4 -> 15 ->6 -> 17 ->8 -> 19

Output:

Linked List: 0 -> 15 -> 19

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

Method:

This method will traverse the given linked list and skip the first m nodes and then delete the next n nodes. It performs this traversing for all the remaining nodes. Here, we need to handle all corner case or boundary conditions and maintain proper links between the nodes of the linked list.

C program to delete N nodes after M nodes of a linked list

 #include<stdio.h>
 #include<stdlib.h>
 // The structure of the node
 struct node
 {
 int info;
 struct node * next;
 };
 struct node * start = NULL;
 // For inserting the elements in the linked list
 void add(int item)
 {
 struct node * t, * p;
 t = (struct node * )malloc( sizeof( struct node ));
 if(start == NULL)
 {
 start = t;
 start -> info = item;
 start -> next = NULL;
 return;
 }
 else
 {
 struct node * p = start;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 }
 }
 // For delete N nodes after M nodes of the linked list
 voidskipMdeleteN(struct node  *head, int M, int N)
 {
 struct node *temp = head, *t;
 int count;
 while (temp)
     {
 for (count = 1; count<M && temp!= NULL; count++)
 temp = temp->next;
 if (temp == NULL)
 return;
          t = temp->next;
 for (count = 1; count<=N && t!= NULL; count++)
         {
 struct Node *temp = t;
             t = t->next;
 free(temp);
         }
 temp->next = t;
 temp = t;
     }
 }
 // To display the elements of the linked list
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     while(t -> next != NULL)
                                     {
                         printf("%d  ->  ", t -> info);
                         t = t -> next;
                         }
                         printf("%d\n", t -> info);
 }
 // Driver Function
 int main()
 {
 int M,N;
 printf("Enter the M and N values:\n");
 scanf("%d%d",&M,&N);
 add(2);
 add(4);
 add(6);
 add(8);
 add(10);
 add(12);
 add(14);
 add(16);
 printf("Linked List before deletion:\n");
 traverse(start);
 skipMdeleteN (start,M,N);
 printf("Linked List after deletion\n");
 traverse(start);
 return 0;
 } 

Output:

Delete N nodes after M nodes of a linked list

Time Complexity: The time complexity of the above method is O(n), where n is the total number of nodes in the linked list.


Related Topics

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

Selection Sort

In each iteration of the selection sort algorithm, the smallest item from an unsorted list is chosen and placed at the top of the unsorted list. Algorithm of Selection Sorting In order...

3 minutes read.

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children...

4 minutes read.

Properties of Binary Tree

Trees are maybe of the most significant datum structures. They are used to store and figure out data. A binarytree is a tree data structure made from nodes, all of which has...

3 minutes read.

Post-order traversal in a binary tree

We all know that postorder is a form of tree traversal to visit the tree's nodes, and it helps us reach out to the tree's nodes. Postorder means visiting the...

4 minutes read.

Trie data structure

Trie data structure The term “trie” comes from the word “retrieval” which means getting information. The trie data structure is a sorted extension of tree-based data structure. The trie data structure...

5 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

3 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Flatten Binary Tree to a linked list

Implementation In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: - // Writing a C++ program that will convert a...

4 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

Right side view of binary tree

The right view of the binary tree is generally known to be that side viewed from the right direction of the point of view. To be more precise, the right-side...

8 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.