Posted on by Kalkicode

# Delete N nodes after M nodes of a linked list

Given an linked list our goal is to delete M nodes after N nodes. That is interesting because we can consider this problem in a two ways.

Consider situation 1: delete node Delete N nodes after M nodes, but in case if deleted last pair are not exist full N nodes. In this case we are also delete existing less than N nodes. For example.

``````Linked List : 1->2->3->4->5->6->7->NULL
Given M=1 , N=3 (deleted node sequence)
So pair of deleted node in this example
(2->3->4) Exactly 3 node delete
(6->7) Last pair are exist less than three nodes (In this method but this are deleted)
After Delete Linked List : 1->5->NULL``````

Suppose we are inserted the following (1,2,3,4,5,6,7,8,9,10) node in a sequence.

In this approach are assume there is possible to delete last sequence are less than given N nodes. If N nodes are exist then this will be deleted. In this case are not compulsory to delete N node complete pair is exist in last sequence.

Test case:Consider the following, test case.

1) When linked list are empty that means we can not delete any nodes. In this case display valid message (like linked list are empty) .

2) Assume that M and N are positive integers. If given value are less then or equal to zero then display valid message (Invalid M , N values).

3) Here M are used to skip node element and N are use to provide how many number of nodes are deleting. For example

``````Case 1:
Given M=3 , N=3 (deleted node sequence)
After Delete Linked List : 1->2->3->NULL

Case 2:

Given M=3 , N=3 (deleted node sequence)
After Delete Linked List : 1->2->3->7->NULL
(note that 7 is pair of M nodes)

Case 3:

Given M=2 , N=3 (deleted node sequence)
After Delete Linked List : 1->2->6->7->NULL
(Note that last deleted is not complete sequence of 3 nodes only two (8,9))``````

Here given code implementation process.

Consider situation 2: Suppose you are write a algorithm which are restrically check delete N nodes of pair . If pair are have exactly N nodes then it will deleted otherwise not deleted this pair.

1) Firstly we are consider given linked list are not empty if there is empty then display valid message. and N and M are valid positive numbers.

2) When found deleted nodes first check this are contain N nodes or not. If N nodes are existing then deleting this nodes. Otherwise not deleting this pair of nodes.

``````Example 1:
Given M=1 , N=3 (deleted node sequence)
So pair of deleted node in this example
(2->3->4) Exactly 3 node delete
(6->7) Last pair are exist less than three nodes (Not deleted)
After Delete Linked List : 1->5->6->7->NULL

Example 2:
Given M=2 , N=2 (deleted node sequence)
After Delete Linked List : 1->2->5->6->NULL

Example 3:
Given M=2 , N=3 (deleted node sequence)
After Delete Linked List : 1->2->6->7->8->9->NULL
(Note that last deleted is not complete sequence of 3 nodes only two (8,9))
``````
``````// Java Program
// Delete N nodes of after the M nodes set B
class Node
{
public int data;
public Node next;
public Node(int value)
{
this.data = value;
this.next = null;
}
}
{
// Class constructors
{
}
// Insert new node at the last
public void insert(int value)
{
// Create new node
Node node = new Node(value);
else
{
// find last node
while (temp.next != null)
{
// visit to next node
temp = temp.next;
}
temp.next = node;
}
}
// Display all Linked List elements
public void display()
{
{
while (temp != null)
{
System.out.print("  " + temp.data);
temp = temp.next;
}
}
else
{
}
}
public void deleteNode(int m, int n)
{
{
}
else if (m <= 0 || n <= 0)
{
System.out.println("Invalid position");
}
else
{
int count = 0, find = 0;
Node temp = this.head, hold = null, current = null;
while (temp != null)
{
if (find < m)
{
//When need to skip element
find++;
hold = temp;
temp = temp.next;
if (find == m - 1 && temp != null)
{
current = temp.next;
find = 0;
// Check it out new deleted sequence are exist or not
while (current != null)
{
find++;
current = current.next;
if (find == n)
{
break;
}
}
if (find != n)
{
// When group of N nodes are not exist
// break the execution process
break;
}
else
{
find = m - 1;
}
}
}
else
{
temp = temp.next;
// Location is to find delete node
if (hold.next != null)
{
// When delete node is found
current = hold.next;
hold.next = current.next;
current = null;
}
count++;
if (count == n)
{
//reset the value
count = 0;
find = 0;
}
}
}
}
}
public static void main(String[] args)
{
int m = 2, n = 3;
System.out.println("\nBefore Delete ");
System.out.println("\n M: " + m + " N: " + n + " ");
System.out.println("After Delete ");
}
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Delete N nodes of after the M nodes set B
class Node
{
public: int data;
Node *next;
Node(int value)
{
this->data = value;
this->next = nullptr;
}
};
{
// Class constructors
{
}
// Insert new node at the last
void insert(int value)
{
// Create new node
Node *node = new Node(value);
{
}
else
{
// find last node
while (temp->next != nullptr)
{
// visit to next node
temp = temp->next;
}
temp->next = node;
}
}
// Display all Linked List elements
void display()
{
{
cout << "Linked List Element :";
while (temp != nullptr)
{
cout << "  " << temp->data;
temp = temp->next;
}
}
else
{
cout << "Empty Linked list" << endl;
}
}
void deleteNode(int m, int n)
{
{
cout << "Empty Linked List" << endl;
}
else if (m <= 0 || n <= 0)
{
cout << "Invalid position" << endl;
}
else
{
int count = 0;
int find = 0;
Node *hold = nullptr;
Node *current = nullptr;
while (temp != nullptr)
{
if (find < m)
{
//When need to skip element
find++;
hold = temp;
temp = temp->next;
if (find == m - 1 && temp != nullptr)
{
current = temp->next;
find = 0;
// Check it out new deleted sequence are exist or not
while (current != nullptr)
{
find++;
current = current->next;
if (find == n)
{
break;
}
}
if (find != n)
{
// When group of N nodes are not exist
// break the execution process
break;
}
else
{
find = m - 1;
}
}
}
else
{
temp = temp->next;
// Location is to find delete node
if (hold->next != nullptr)
{
// When delete node is found
current = hold->next;
hold->next = current->next;
current = nullptr;
}
count++;
if (count == n)
{
//reset the value
count = 0;
find = 0;
}
}
}
}
}
};
int main()
{
int m = 2;
int n = 3;
cout << "\nBefore Delete " << endl;
cout << "\n M: " << m << " N: " << n << " " << endl;
cout << "After Delete " << endl;
return 0;
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````package main
import "fmt"
// Go Program
// Delete N nodes of after the M nodes set B
type Node struct {
data int
next * Node
}
func getNode(value int) * Node {
return &Node {value,nil}
}
}
}
// Insert new node at the last
// Create new node
var node * Node = getNode(value)
} else {
var temp * Node = this.head
// find last node
for (temp.next != nil) {
// visit to next node
temp = temp.next
}
temp.next = node
}
}
// Display all Linked List elements
var temp * Node = this.head
for (temp != nil) {
fmt.Print("  ", temp.data)
temp = temp.next
}
} else {
}
}
func(this LinkedList) deleteNode(m, n int) {
} else if m <= 0 || n <= 0 {
fmt.Println("Invalid position")
} else {
var count int = 0
var find int = 0
var temp * Node = this.head
var hold * Node = nil
var current * Node = nil
for (temp != nil) {
if find < m {
//When need to skip element
find++
hold = temp
temp = temp.next
if find == m - 1 && temp != nil {
current = temp.next
find = 0
// Check it out new deleted sequence are exist or not
for (current != nil) {
find++
current = current.next
if find == n {
break
}
}
if find != n {
// When group of N nodes are not exist
// break the execution process
break
} else {
find = m - 1
}
}
} else {
temp = temp.next
// Location is to find delete node
if hold.next != nil {
// When delete node is found
current = hold.next
hold.next = current.next
current = nil
}
count++
if count == n {
//reset the value
count = 0
find = 0
}
}
}
}
}
func main() {
var m int = 2
var n int = 3
fmt.Println("\nBefore Delete ")
fmt.Println("\n M: ", m, " N: ", n, " ")
fmt.Println("After Delete ")
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````//C Program to Delete node N nodes after M node
//method 2
#include<stdio.h>
#include <stdlib.h> //for malloc function
// Create structure
struct Node
{
int data;
struct Node *next;
};
// Function prototype
void insert(struct Node **, int);
void display(struct Node *);
void delete_node(struct Node *, int, int);
//insert Node element
void insert(struct Node **head, int value)
{
// Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
node->data = value;
node->next = NULL;
{
}
else
{
// find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// add node at last position
temp->next = node;
}
}
}
// display element of Node
void display(struct Node *temp)
{
if (temp == NULL)
{
}
while (temp != NULL)
{
printf("%d  ", temp->data);
temp = temp->next;
}
}
// delete N nodes after M nodes
void delete_node(struct Node *head, int M, int N)
{
{
}
else if (M <= 0 || N <= 0)
{
printf("\nInvalid positions");
}
else
{
int count = 0, find = 0;
struct Node *temp = head, *hold = NULL, *current = NULL;
while (temp != NULL)
{
if (find < M)
{
// When need to skip element
find++;
hold = temp;
temp = temp->next;
if (find == M - 1 && temp != NULL)
{
current = temp->next;
find = 0;
// check it out new deleted sequence are exist or not
while (current != NULL)
{
find++;
current = current->next;
if (find == N)
{
break;
}
}
if (find != N)
{
// when group of N nodes are not exist
// break the execution process
break;
}
else
{
find = M - 1;
}
}
}
else
{
temp = temp->next;
// location is to find delete node
if (hold->next != NULL)
{
// when delete node is found
current = hold->next;
hold->next = current->next;
free(current);
current = NULL;
}
count++;
if (count == N)
{
// reset the value of find and count nodes
count = 0;
find = 0;
}
}
}
}
}
int main()
{
// Create node pointer
int m = 2, n = 3;
// Insert element of linked list
// Display all node
printf("\n M = %d  N = %d", m, n);
printf("\nAfter Remove Linked List : ");
}``````

#### Output

``````Linked List :1  2  3  4  5  6  7  8  9
M = 2  N = 3
After Remove Linked List : 1  2  6  7  8  9``````
``````// Include namespace system
using System;
// Csharp Program
// Delete N nodes of after the M nodes set B
public class Node
{
public int data;
public Node next;
public Node(int value)
{
this.data = value;
this.next = null;
}
}
{
// Class constructors
{
}
// Insert new node at the last
public void insert(int value)
{
// Create new node
var node = new Node(value);
{
}
else
{
// find last node
while (temp.next != null)
{
// visit to next node
temp = temp.next;
}
temp.next = node;
}
}
// Display all Linked List elements
public void display()
{
{
while (temp != null)
{
Console.Write("  " + temp.data.ToString());
temp = temp.next;
}
}
else
{
}
}
public void deleteNode(int m, int n)
{
{
}
else if (m <= 0 || n <= 0)
{
Console.WriteLine("Invalid position");
}
else
{
var count = 0;
var find = 0;
Node hold = null;
Node current = null;
while (temp != null)
{
if (find < m)
{
//When need to skip element
find++;
hold = temp;
temp = temp.next;
if (find == m - 1 && temp != null)
{
current = temp.next;
find = 0;
// Check it out new deleted sequence are exist or not
while (current != null)
{
find++;
current = current.next;
if (find == n)
{
break;
}
}
if (find != n)
{
// When group of N nodes are not exist
// break the execution process
break;
}
else
{
find = m - 1;
}
}
}
else
{
temp = temp.next;
// Location is to find delete node
if (hold.next != null)
{
// When delete node is found
current = hold.next;
hold.next = current.next;
current = null;
}
count++;
if (count == n)
{
//reset the value
count = 0;
find = 0;
}
}
}
}
}
public static void Main(String[] args)
{
var m = 2;
var n = 3;
Console.WriteLine("\nBefore Delete ");
Console.WriteLine("\n M: " + m.ToString() + " N: " + n.ToString() + " ");
Console.WriteLine("After Delete ");
}
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````<?php
// Php Program
// Delete N nodes of after the M nodes set B
class Node
{
public \$data;
public \$next;
public	function __construct(\$value)
{
\$this->data = \$value;
\$this->next = NULL;
}
}

{
// Class constructors
public	function __construct()
{
}
// Insert new node at the last
public	function insert(\$value)
{
// Create new node
\$node = new Node(\$value);
{
}
else
{
// find last node
while (\$temp->next != NULL)
{
// visit to next node
\$temp = \$temp->next;
}
\$temp->next = \$node;
}
}
// Display all Linked List elements
public	function display()
{
{
while (\$temp != NULL)
{
printf("%s", "  ".strval(\$temp->data));
\$temp = \$temp->next;
}
}
else
{
}
}
public	function deleteNode(\$m, \$n)
{
{
}
else if (\$m <= 0 || \$n <= 0)
{
printf("%s\n", "Invalid position");
}
else
{
\$count = 0;
\$find = 0;
\$hold = NULL;
\$current = NULL;
while (\$temp != NULL)
{
if (\$find < \$m)
{
//When need to skip element
\$find++;
\$hold = \$temp;
\$temp = \$temp->next;
if (\$find == \$m - 1 && \$temp != NULL)
{
\$current = \$temp->next;
\$find = 0;
// Check it out new deleted sequence are exist or not
while (\$current != NULL)
{
\$find++;
\$current = \$current->next;
if (\$find == \$n)
{
break;
}
}
if (\$find != \$n)
{
// When group of N nodes are not exist
// break the execution process
break;
}
else
{
\$find = \$m - 1;
}
}
}
else
{
\$temp = \$temp->next;
// Location is to find delete node
if (\$hold->next != NULL)
{
// When delete node is found
\$current = \$hold->next;
\$hold->next = \$current->next;
\$current = NULL;
}
\$count++;
if (\$count == \$n)
{
//reset the value
\$count = 0;
\$find = 0;
}
}
}
}
}
public static
function main(\$args)
{
\$m = 2;
\$n = 3;
printf("%s\n", "\nBefore Delete ");
printf("%s\n", "\n M: ".strval(\$m).
" N: ".strval(\$n).
" ");
printf("%s\n", "After Delete ");
}
}

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````// Node JS Program
// Delete N nodes of after the M nodes set B
class Node
{
constructor(value)
{
this.data = value;
this.next = null;
}
}
{
// Class constructors
constructor()
{
}
// Insert new node at the last
insert(value)
{
// Create new node
var node = new Node(value);
{
}
else
{
// find last node
while (temp.next != null)
{
// visit to next node
temp = temp.next;
}
temp.next = node;
}
}
// Display all Linked List elements
display()
{
{
while (temp != null)
{
process.stdout.write("  " + temp.data);
temp = temp.next;
}
}
else
{
}
}
deleteNode(m, n)
{
{
}
else if (m <= 0 || n <= 0)
{
console.log("Invalid position");
}
else
{
var count = 0;
var find = 0;
var hold = null;
var current = null;
while (temp != null)
{
if (find < m)
{
//When need to skip element
find++;
hold = temp;
temp = temp.next;
if (find == m - 1 && temp != null)
{
current = temp.next;
find = 0;
// Check it out new deleted sequence are exist or not
while (current != null)
{
find++;
current = current.next;
if (find == n)
{
break;
}
}
if (find != n)
{
// When group of N nodes are not exist
// break the execution process
break;
}
else
{
find = m - 1;
}
}
}
else
{
temp = temp.next;
// Location is to find delete node
if (hold.next != null)
{
// When delete node is found
current = hold.next;
hold.next = current.next;
current = null;
}
count++;
if (count == n)
{
//reset the value
count = 0;
find = 0;
}
}
}
}
}
}

function main()
{
var m = 2;
var n = 3;
console.log("\nBefore Delete ");
console.log("\n M: " + m + " N: " + n + " ");
console.log("After Delete ");
}
// Start program execution
main();``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````#  Python 3 Program
#  Delete N nodes of after the M nodes set B
class Node :
def __init__(self, value) :
self.data = value
self.next = None

#  Class constructors
def __init__(self) :

#  Insert new node at the last
def insert(self, value) :
#  Create new node
node = Node(value)
else :
#  find last node
while (temp.next != None) :
#  visit to next node
temp = temp.next

temp.next = node

#  Display all Linked List elements
def display(self) :
print("Linked List Element :", end = "")
while (temp != None) :
print(" ", temp.data, end = " ")
temp = temp.next

else :

def deleteNode(self, m, n) :
elif (m <= 0 or n <= 0) :
print("Invalid position")
else :
count = 0
find = 0
hold = None
current = None
while (temp != None) :
if (find < m) :
# When need to skip element
find += 1
hold = temp
temp = temp.next
if (find == m - 1 and temp != None) :
current = temp.next
find = 0
#  Check it out new deleted sequence are exist or not
while (current != None) :
find += 1
current = current.next
if (find == n) :
break

if (find != n) :
#  When group of N nodes are not exist
#  break the execution process
break
else :
find = m - 1

else :
temp = temp.next
#  Location is to find delete node
if (hold.next != None) :
#  When delete node is found
current = hold.next
hold.next = current.next
current = None

count += 1
if (count == n) :
# reset the value
count = 0
find = 0

def main() :
m = 2
n = 3
print("\nBefore Delete ")
print("\n M: ", m ," N: ", n ," ")
print("After Delete ")

if __name__ == "__main__": main()``````

#### Output

``````Before Delete
Linked List Element :  1   2   3   4   5   6   7   8   9
M:  2  N:  3
After Delete
Linked List Element :  1   2   6   7   8   9``````
``````#  Ruby Program
#  Delete N nodes of after the M nodes set B
class Node
# Define the accessor and reader of class Node
attr_accessor :data, :next
def initialize(value)
self.data = value
self.next = nil
end

end

#  Class constructors
def initialize()
end

#  Insert new node at the last
def insert(value)
#  Create new node
node = Node.new(value)
else

#  find last node
while (temp.next != nil)
#  visit to next node
temp = temp.next
end

temp.next = node
end

end

#  Display all Linked List elements
def display()
while (temp != nil)
print("  ", temp.data)
temp = temp.next
end

else

end

end

def deleteNode(m, n)
elsif (m <= 0 || n <= 0)
print("Invalid position", "\n")
else

count = 0
find = 0
hold = nil
current = nil
while (temp != nil)
if (find < m)
# When need to skip element
find += 1
hold = temp
temp = temp.next
if (find == m - 1 && temp != nil)
current = temp.next
find = 0
#  Check it out new deleted sequence are exist or not
while (current != nil)
find += 1
current = current.next
if (find == n)
break
end

end

if (find != n)
#  When group of N nodes are not exist
#  break the execution process
break
else

find = m - 1
end

end

else

temp = temp.next
#  Location is to find delete node
if (hold.next != nil)
#  When delete node is found
current = hold.next
hold.next = current.next
current = nil
end

count += 1
if (count == n)
# reset the value
count = 0
find = 0
end

end

end

end

end

end

def main()
m = 2
n = 3
print("\nBefore Delete ", "\n")
print("\n M: ", m ," N: ", n ," ", "\n")
print("After Delete ", "\n")
end

main()``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````// Scala Program
// Delete N nodes of after the M nodes set B
class Node(var data: Int,
var next: Node)
{
def this(value: Int)
{
this(value, null);
}
}
{
// Class constructors
def this()
{
this(null);
}
// Insert new node at the last
def insert(value: Int): Unit = {
// Create new node
var node: Node = new Node(value);
{
}
else
{
// find last node
while (temp.next != null)
{
// visit to next node
temp = temp.next;
}
temp.next = node;
}
}
// Display all Linked List elements
def display(): Unit = {
{
while (temp != null)
{
print("  " + temp.data);
temp = temp.next;
}
}
else
{
}
}
def deleteNode(m: Int, n: Int): Unit = {
{
}
else if (m <= 0 || n <= 0)
{
println("Invalid position");
}
else
{
var count: Int = 0;
var find: Int = 0;
var hold: Node = null;
var current: Node = null;
while (temp != null)
{
if (find < m)
{
//When need to skip element
find += 1;
hold = temp;
temp = temp.next;
if (find == m - 1 && temp != null)
{
current = temp.next;
find = 0;
// Check it out new deleted sequence are exist or not
while (current != null && find < n)
{
find += 1;
current = current.next;
}
if (find != n)
{
// When group of N nodes are not exist
// break the execution process
return;
}
else
{
find = m - 1;
}
}
}
else
{
temp = temp.next;
// Location is to find delete node
if (hold.next != null)
{
// When delete node is found
current = hold.next;
hold.next = current.next;
current = null;
}
count += 1;
if (count == n)
{
//reset the value
count = 0;
find = 0;
}
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var m: Int = 2;
var n: Int = 3;
println("\nBefore Delete ");
println("\n M: " + m + " N: " + n + " ");
println("After Delete ");
}
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````import Foundation
// Java Program
// Delete N nodes of after the M nodes set B
class Node
{
var data: Int;
var next: Node? ;
init(_ value: Int)
{
self.data = value;
self.next = nil;
}
}
{
// Class constructors
init()
{
}
// Insert new node at the last
func insert(_ value: Int)
{
// Create new node
let node: Node? = Node(value);
{
}
else
{
// find last node
while (temp!.next  != nil)
{
// visit to next node
temp = temp!.next;
}
temp!.next = node;
}
}
// Display all Linked List elements
func display()
{
{
print("Linked List Element :", terminator: "");
while (temp  != nil)
{
print("  "
+ String(temp!.data), terminator: "");
temp = temp!.next;
}
}
else
{
}
}
func deleteNode(_ m: Int, _ n: Int)
{
{
}
else if (m <= 0 || n <= 0)
{
print("Invalid position");
}
else
{
var count: Int = 0;
var find: Int = 0;
var hold: Node? = nil;
var current: Node? = nil;
while (temp  != nil)
{
if (find < m)
{
//When need to skip element
find += 1;
hold = temp;
temp = temp!.next;
if (find == m - 1 && temp  != nil)
{
current = temp!.next;
find = 0;
// Check it out new deleted sequence are exist or not
while (current  != nil)
{
find += 1;
current = current!.next;
if (find == n)
{
break;
}
}
if (find  != n)
{
// When group of N nodes are not exist
// break the execution process
break;
}
else
{
find = m - 1;
}
}
}
else
{
temp = temp!.next;
// Location is to find delete node
if (hold!.next  != nil)
{
// When delete node is found
current = hold!.next;
hold!.next = current!.next;
current = nil;
}
count += 1;
if (count == n)
{
//reset the value
count = 0;
find = 0;
}
}
}
}
}
static func main(_ args: [String])
{
let m: Int = 2;
let n: Int = 3;
print("\nBefore Delete ");
print("\n M: "
+ String(m) + " N: "
+ String(n) + " ");
print("After Delete ");
}
}

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````
``````// Kotlin Program
// Delete N nodes of after the M nodes set B
class Node
{
var data: Int;
var next: Node ? ;
constructor(value: Int)
{
this.data = value;
this.next = null;
}
}
{
// Class constructors
constructor()
{
}
// Insert new node at the last
fun insert(value: Int): Unit
{
// Create new node
val node: Node = Node(value);
{
}
else
{
var temp: Node ? = this.head;
// find last node
while (temp?.next != null)
{
// visit to next node
temp = temp.next;
}
temp?.next = node;
}
}
// Display all Linked List elements
fun display(): Unit
{
{
var temp: Node ? = this.head;
while (temp != null)
{
print("  " + temp.data);
temp = temp.next;
}
}
else
{
}
}
fun deleteNode(m: Int, n: Int): Unit
{
{
}
else if (m <= 0 || n <= 0)
{
println("Invalid position");
}
else
{
var count: Int = 0;
var find: Int = 0;
var temp: Node ? = this.head;
var hold: Node ? = null;
var current: Node ? ;
while (temp != null)
{
if (find < m)
{
//When need to skip element
find += 1;
hold = temp;
temp = temp.next;
if (find == m - 1 && temp != null)
{
current = temp.next;
find = 0;
// Check it out new deleted sequence are exist or not
while (current != null)
{
find += 1;
current = current.next;
if (find == n)
{
break;
}
}
if (find != n)
{
// When group of N nodes are not exist
// break the execution process
break;
}
else
{
find = m - 1;
}
}
}
else
{
temp = temp.next;
// Location is to find delete node
if (hold?.next != null)
{
// When delete node is found
current = hold.next;
hold.next = current?.next;

}
count += 1;
if (count == n)
{
//reset the value
count = 0;
find = 0;
}
}
}
}
}
}
fun main(args: Array < String > ): Unit
{
val m: Int = 2;
val n: Int = 3;
println("\nBefore Delete ");
println("\n M: " + m + " N: " + n + " ");
println("After Delete ");
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  3  4  5  6  7  8  9
M: 2 N: 3
After Delete
Linked List Element :  1  2  6  7  8  9``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post