Delete N After Every M in a Linked List

Last Updated : 28 Jul, 2026

Given a linked list, delete n nodes after skipping m nodes of a linked list until the last of the linked list.

Examples

Input: head: 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: head: 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.

Try It Yourself
redirect icon

Traverse the List and Update Links - O(n) Time and O(1) Space

The idea is to traverse the linked list while repeatedly keeping the first m nodes and deleting the next n nodes. After skipping m nodes, simply update the next pointer of the last retained node to bypass the next n nodes. Repeat this process until the end of the list is reached.

Working of Approach:

  • Initialize a pointer temp to the head of the linked list.
  • Move temp forward by m - 1 nodes so that it points to the last node to be retained.
  • If the end of the list is reached, stop the traversal.
  • Store the node immediately after temp in another pointer.
  • Move this pointer forward by n nodes to skip the nodes that need to be deleted.
  • Update temp -> next to point to the first node after the deleted nodes.
  • Move temp to the next retained node and repeat the process until the end of the list is reached.
C++
#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int val) {
        data = val;
        next = nullptr;
    }
};

void linkDelete(Node* head, int n, int m) {
    Node* temp = head;
    Node* t;

    while (temp != nullptr) {

        // Skip the first m nodes
        for (int count = 1; count < m && temp != nullptr; count++)
            temp = temp->next;

        if (temp == nullptr)
            return;

        // Move to the first node to be deleted
        t = temp->next;

        // Skip the next n nodes
        for (int count = 1; count <= n && t != nullptr; count++)
            t = t->next;

        // Link the retained nodes with the remaining list
        temp->next = t;

        temp = t;
    }
}

void printList(Node* head) {
    while (head != nullptr) {
        cout << head->data;
        if (head->next != nullptr)
            cout << " -> ";
        head = head->next;
    }
    cout << '\n';
}

int main() {

    Node* head = new Node(9);
    head->next = new Node(1);
    head->next->next = new Node(3);
    head->next->next->next = new Node(5);
    head->next->next->next->next = new Node(9);
    head->next->next->next->next->next = new Node(4);
    head->next->next->next->next->next->next = new Node(10);
    head->next->next->next->next->next->next->next = new Node(1);

    int m = 2;
    int n = 1;

    linkDelete(head, n, m);

    printList(head);

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class GFG {

    static void linkDelete(Node head, int n, int m) {
        Node temp = head;
        Node t;

        while (temp != null) {

            // Skip the first m nodes
            for (int count = 1; count < m && temp != null; count++)
                temp = temp.next;

            if (temp == null)
                return;

            // Move to the first node to be deleted
            t = temp.next;

            // Skip the next n nodes
            for (int count = 1; count <= n && t != null; count++)
                t = t.next;

            // Link the retained nodes with the remaining list
            temp.next = t;

            temp = t;
        }
    }

    static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data);
            if (head.next != null)
                System.out.print(" -> ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        Node head = new Node(9);
        head.next = new Node(1);
        head.next.next = new Node(3);
        head.next.next.next = new Node(5);
        head.next.next.next.next = new Node(9);
        head.next.next.next.next.next = new Node(4);
        head.next.next.next.next.next.next = new Node(10);
        head.next.next.next.next.next.next.next = new Node(1);

        int m = 2;
        int n = 1;

        linkDelete(head, n, m);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


def linkDelete(head, n, m):
    temp = head

    while temp:

        # Skip the first m nodes
        for _ in range(1, m):
            if temp is None:
                return
            temp = temp.next

        if temp is None:
            return

        # Move to the first node to be deleted
        t = temp.next

        # Skip the next n nodes
        for _ in range(n):
            if t is None:
                break
            t = t.next

        # Link the retained nodes with the remaining list
        temp.next = t

        temp = t


def printList(head):
    while head:
        print(head.data, end="")
        if head.next:
            print(" -> ", end="")
        head = head.next
    print()


if __name__ == "__main__":

    head = Node(9)
    head.next = Node(1)
    head.next.next = Node(3)
    head.next.next.next = Node(5)
    head.next.next.next.next = Node(9)
    head.next.next.next.next.next = Node(4)
    head.next.next.next.next.next.next = Node(10)
    head.next.next.next.next.next.next.next = Node(1)

    m = 2
    n = 1

    linkDelete(head, n, m)

    printList(head)
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class GFG {

    static void linkDelete(Node head, int n, int m) {
        Node temp = head;
        Node t;

        while (temp != null) {

            // Skip the first m nodes
            for (int count = 1; count < m && temp != null; count++)
                temp = temp.next;

            if (temp == null)
                return;

            // Move to the first node to be deleted
            t = temp.next;

            // Skip the next n nodes
            for (int count = 1; count <= n && t != null; count++)
                t = t.next;

            // Link the retained nodes with the remaining list
            temp.next = t;

            temp = t;
        }
    }

    static void PrintList(Node head) {
        while (head != null) {
            Console.Write(head.data);
            if (head.next != null)
                Console.Write(" -> ");
            head = head.next;
        }
        Console.WriteLine();
    }

    static void Main() {

        Node head = new Node(9);
        head.next = new Node(1);
        head.next.next = new Node(3);
        head.next.next.next = new Node(5);
        head.next.next.next.next = new Node(9);
        head.next.next.next.next.next = new Node(4);
        head.next.next.next.next.next.next = new Node(10);
        head.next.next.next.next.next.next.next = new Node(1);

        int m = 2;
        int n = 1;

        linkDelete(head, n, m);

        PrintList(head);
    }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function linkDelete(head, n, m) {
    let temp = head;

    while (temp !== null) {

        // Skip the first m nodes
        for (let count = 1; count < m && temp !== null; count++)
            temp = temp.next;

        if (temp === null)
            return;

        // Move to the first node to be deleted
        let t = temp.next;

        // Skip the next n nodes
        for (let count = 1; count <= n && t !== null; count++)
            t = t.next;

        // Link the retained nodes with the remaining list
        temp.next = t;

        temp = t;
    }
}

function printList(head) {
    let ans = [];

    while (head !== null) {
        ans.push(head.data);
        head = head.next;
    }

    console.log(ans.join(" -> "));
}

// Driver Code
let head = new Node(9);
head.next = new Node(1);
head.next.next = new Node(3);
head.next.next.next = new Node(5);
head.next.next.next.next = new Node(9);
head.next.next.next.next.next = new Node(4);
head.next.next.next.next.next.next = new Node(10);
head.next.next.next.next.next.next.next = new Node(1);

let m = 2;
let n = 1;

linkDelete(head, n, m);

printList(head);

Output
9 -> 1 -> 5 -> 9 -> 10 -> 1
Comment