Delete N nodes after M nodes of a Linked List

Introduction

The linked list is one of the most important data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

Problem Statement

In this question, we are given a singly linked list, a value M and a value N. We have to delete N nodes after M nodes of the given list. This means that we will keep M nodes, and delete N nodes, and will continue this till we reach the end of the linked list.

Problem Statement Understanding

Let the given list be 1 -> 2 -> 3 -> 4 -> 56 -> 45 -> 7 -> 8 and the value of M=2 and N=2. This means that after every 2 node, we will delete 2 nodes.

So, in the first step, our linked list will be 1 -> 2 -> 56 -> 45 -> 7 -> 8 . As we can see, we have removed the 3rd and 4th nodes from the list. Now, in the second step, our linked list will be 1 -> 2 -> 56 -> 45. Now, we have reached the tail of the list.

So, the final linked list is 1 - > 2 - > 56 - > 45.

Input: M=2, N=2

Output:

Explanation: As the value of M is 2 and the value of N is 2, we will retain 2 nodes and delete 2 nodes till we reach the end of the list.

This is an interesting question. It is not a complex one, but we have to look out for all the edge cases. Let us have a glance at the approach.

Approach

We are going to make use of list traversal here. First, we will skip M nodes. Now, if we reach the end of the list, then we will terminate the method, else, we will store the address of the Mth node in the current and temp will point to the (M+1)th node which is next of current. Now, we will traverse the next N nodes, and increment temp. In the end, we will make the Mth node point to the temp, and temp will become our new current.

Algorithm

  • Create a node current, and make it point to the head.
  • Traverse through the list till the end is reached.
  • Skip M nodes. We can do this with the help of a for loop. In every iteration, do current = current -> next.
  • Now, if the current becomes NULL, it means that we have reached the end of the list. We will terminate the method.
  • Else, store the next of current in temp. Write a for loop that runs N times, and increment temp by 1 in each iteration.
  • After the loop, make the current point to temp.
  • The temp will become our new current.

Dry Run

Code Implementation


#include 
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<data<<" ";
        temp = temp->next;
    }
    cout<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=2;
    push(&head, 8);
    push(&head, 7);
    push(&head, 45);
    push(&head, 56);
    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;
}


import java.util.*;

public class PrepBytes
{

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)
    {
        // Skip M nodes
        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=2;
    head=push(head, 8);
    head=push(head, 7);
    head=push(head, 45);
    head=push(head, 56);
    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);
}
}

Output

Given Linked list
1 2 3 4 56 45 7 8

Linked list after deletion
1 2 56 45

Time Complexity: O(n), as no nested traversal is needed.
Space Complexity: O(1), as only temporary variables are being created.

So, in this article, we have tried to explain the most efficient approach to delete N nodes after M nodes of a linked list. This is an important question when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post Swap nodes in a linked list without swapping data
Next post The intersection of two Sorted Linked Lists

Leave a Reply

Your email address will not be published. Required fields are marked *