Skip to main content

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:
Linked List : 1->2->3->4->5->6->NULL
Given M=3 , N=3 (deleted node sequence)
After Delete Linked List : 1->2->3->NULL

Case 2:

Linked List : 1->2->3->4->5->6->7->NULL
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:

Linked List : 1->2->3->4->5->6->7->8->9->NULL
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))
Before Delete N nodes after M nodes After Remove N=3 nodes after M=2 nodes

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:
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 (Not deleted)
After Delete Linked List : 1->5->6->7->NULL


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


Example 3:
Linked List : 1->2->3->4->5->6->7->8->9->NULL
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;
	}
}
public class LinkedList
{
	public Node head;
	// Class constructors
	public LinkedList()
	{
		head = null;
	}
	// Insert new node at the last
	public void insert(int value)
	{
		// Create new node
		Node node = new Node(value);
		if (head == null) head = node;
		else
		{
			Node temp = head;
			// find last node
			while (temp.next != null)
			{
				// visit to next node
				temp = temp.next;
			}
			// add node
			temp.next = node;
		}
	}
	// Display all Linked List elements
	public void display()
	{
		if (this.head != null)
		{
			System.out.print("Linked List Element :");
			Node temp = head;
			while (temp != null)
			{
				System.out.print("  " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			System.out.println("Empty Linked list");
		}
	}
	public void deleteNode(int m, int n)
	{
		if (this.head == null)
		{
			System.out.println("Empty Linked List");
		}
		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;
		LinkedList task = new LinkedList();
		task.insert(1);
		task.insert(2);
		task.insert(3);
		task.insert(4);
		task.insert(5);
		task.insert(6);
		task.insert(7);
		task.insert(8);
		task.insert(9);
		System.out.println("\nBefore Delete ");
		task.display();
		System.out.println("\n M: " + m + " N: " + n + " ");
		System.out.println("After Delete ");
		task.deleteNode(m, n);
		task.display();
	}
}

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 LinkedList
{
	public: Node *head;
	// Class constructors
	LinkedList()
	{
		this->head = nullptr;
	}
	// Insert new node at the last
	void insert(int value)
	{
		// Create new node
		Node *node = new Node(value);
		if (this->head == nullptr)
		{
			this->head = node;
		}
		else
		{
			Node *temp = this->head;
			// find last node
			while (temp->next != nullptr)
			{
				// visit to next node
				temp = temp->next;
			}
			// add node
			temp->next = node;
		}
	}
	// Display all Linked List elements
	void display()
	{
		if (this->head != nullptr)
		{
			cout << "Linked List Element :";
			Node *temp = this->head;
			while (temp != nullptr)
			{
				cout << "  " << temp->data;
				temp = temp->next;
			}
		}
		else
		{
			cout << "Empty Linked list" << endl;
		}
	}
	void deleteNode(int m, int n)
	{
		if (this->head == nullptr)
		{
			cout << "Empty Linked List" << endl;
		}
		else if (m <= 0 || n <= 0)
		{
			cout << "Invalid position" << endl;
		}
		else
		{
			int count = 0;
			int find = 0;
			Node *temp = this->head;
			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;
	LinkedList *task = new LinkedList();
	task->insert(1);
	task->insert(2);
	task->insert(3);
	task->insert(4);
	task->insert(5);
	task->insert(6);
	task->insert(7);
	task->insert(8);
	task->insert(9);
	cout << "\nBefore Delete " << endl;
	task->display();
	cout << "\n M: " << m << " N: " << n << " " << endl;
	cout << "After Delete " << endl;
	task->deleteNode(m, n);
	task->display();
	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}
}
type LinkedList struct {
	head * Node
}
func getLinkedList() * LinkedList {
	return &LinkedList {nil}
}
// Insert new node at the last
func(this *LinkedList) insert(value int) {
	// Create new node
	var node * Node = getNode(value)
	if this.head == nil {
		this.head = node
	} else {
		var temp * Node = this.head
		// find last node
		for (temp.next != nil) {
			// visit to next node
			temp = temp.next
		}
		// add node
		temp.next = node
	}
}
// Display all Linked List elements
func(this LinkedList) display() {
	if this.head != nil {
		fmt.Print("Linked List Element :")
		var temp * Node = this.head
		for (temp != nil) {
			fmt.Print("  ", temp.data)
			temp = temp.next
		}
	} else {
		fmt.Println("Empty Linked list")
	}
}
func(this LinkedList) deleteNode(m, n int) {
	if this.head == nil {
		fmt.Println("Empty Linked List")
	} 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
	var task * LinkedList = getLinkedList()
	task.insert(1)
	task.insert(2)
	task.insert(3)
	task.insert(4)
	task.insert(5)
	task.insert(6)
	task.insert(7)
	task.insert(8)
	task.insert(9)
	fmt.Println("\nBefore Delete ")
	task.display()
	fmt.Println("\n M: ", m, " N: ", n, " ")
	fmt.Println("After Delete ")
	task.deleteNode(m, n)
	task.display()
}

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;
		if ( *head == NULL)
		{
			*head = node;
		}
		else
		{
			struct Node *temp = *head;
			// 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)
	{
		printf("\nEmpty linked list");
	}
	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)
{
	if (head == NULL)
	{
		printf("\nEmpty Linked List\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
	struct Node *head = NULL;
	int m = 2, n = 3;
	// Insert element of linked list
	insert( &head, 1);
	insert( &head, 2);
	insert( &head, 3);
	insert( &head, 4);
	insert( &head, 5);
	insert( &head, 6);
	insert( &head, 7);
	insert( &head, 8);
	insert( &head, 9);
	printf("Linked List :");
	// Display all node
	display(head);
	printf("\n M = %d  N = %d", m, n);
	printf("\nAfter Remove Linked List : ");
	delete_node(head, m, n);
	display(head);
}

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;
	}
}
public class LinkedList
{
	public Node head;
	// Class constructors
	public LinkedList()
	{
		this.head = null;
	}
	// Insert new node at the last
	public void insert(int value)
	{
		// Create new node
		var node = new Node(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			var temp = this.head;
			// find last node
			while (temp.next != null)
			{
				// visit to next node
				temp = temp.next;
			}
			// add node
			temp.next = node;
		}
	}
	// Display all Linked List elements
	public void display()
	{
		if (this.head != null)
		{
			Console.Write("Linked List Element :");
			var temp = this.head;
			while (temp != null)
			{
				Console.Write("  " + temp.data.ToString());
				temp = temp.next;
			}
		}
		else
		{
			Console.WriteLine("Empty Linked list");
		}
	}
	public void deleteNode(int m, int n)
	{
		if (this.head == null)
		{
			Console.WriteLine("Empty Linked List");
		}
		else if (m <= 0 || n <= 0)
		{
			Console.WriteLine("Invalid position");
		}
		else
		{
			var count = 0;
			var find = 0;
			var temp = this.head;
			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;
		var task = new LinkedList();
		task.insert(1);
		task.insert(2);
		task.insert(3);
		task.insert(4);
		task.insert(5);
		task.insert(6);
		task.insert(7);
		task.insert(8);
		task.insert(9);
		Console.WriteLine("\nBefore Delete ");
		task.display();
		Console.WriteLine("\n M: " + m.ToString() + " N: " + n.ToString() + " ");
		Console.WriteLine("After Delete ");
		task.deleteNode(m, n);
		task.display();
	}
}

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 LinkedList
{
	public $head;
	// Class constructors
	public	function __construct()
	{
		$this->head = NULL;
	}
	// Insert new node at the last
	public	function insert($value)
	{
		// Create new node
		$node = new Node($value);
		if ($this->head == NULL)
		{
			$this->head = $node;
		}
		else
		{
			$temp = $this->head;
			// find last node
			while ($temp->next != NULL)
			{
				// visit to next node
				$temp = $temp->next;
			}
			// add node
			$temp->next = $node;
		}
	}
	// Display all Linked List elements
	public	function display()
	{
		if ($this->head != NULL)
		{
			printf("%s", "Linked List Element :");
			$temp = $this->head;
			while ($temp != NULL)
			{
				printf("%s", "  ".strval($temp->data));
				$temp = $temp->next;
			}
		}
		else
		{
			printf("%s\n", "Empty Linked list");
		}
	}
	public	function deleteNode($m, $n)
	{
		if ($this->head == NULL)
		{
			printf("%s\n", "Empty Linked List");
		}
		else if ($m <= 0 || $n <= 0)
		{
			printf("%s\n", "Invalid position");
		}
		else
		{
			$count = 0;
			$find = 0;
			$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
	function main($args)
	{
		$m = 2;
		$n = 3;
		$task = new LinkedList();
		$task->insert(1);
		$task->insert(2);
		$task->insert(3);
		$task->insert(4);
		$task->insert(5);
		$task->insert(6);
		$task->insert(7);
		$task->insert(8);
		$task->insert(9);
		printf("%s\n", "\nBefore Delete ");
		$task->display();
		printf("%s\n", "\n M: ".strval($m).
			" N: ".strval($n).
			" ");
		printf("%s\n", "After Delete ");
		$task->deleteNode($m, $n);
		$task->display();
	}
}

LinkedList::main(array());

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 LinkedList
{
	// Class constructors
	constructor()
	{
		this.head = null;
	}
	// Insert new node at the last
	insert(value)
	{
		// Create new node
		var node = new Node(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			var temp = this.head;
			// find last node
			while (temp.next != null)
			{
				// visit to next node
				temp = temp.next;
			}
			// add node
			temp.next = node;
		}
	}
	// Display all Linked List elements
	display()
	{
		if (this.head != null)
		{
			process.stdout.write("Linked List Element :");
			var temp = this.head;
			while (temp != null)
			{
				process.stdout.write("  " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			console.log("Empty Linked list");
		}
	}
	deleteNode(m, n)
	{
		if (this.head == null)
		{
			console.log("Empty Linked List");
		}
		else if (m <= 0 || n <= 0)
		{
			console.log("Invalid position");
		}
		else
		{
			var count = 0;
			var find = 0;
			var temp = this.head;
			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;
	var task = new LinkedList();
	task.insert(1);
	task.insert(2);
	task.insert(3);
	task.insert(4);
	task.insert(5);
	task.insert(6);
	task.insert(7);
	task.insert(8);
	task.insert(9);
	console.log("\nBefore Delete ");
	task.display();
	console.log("\n M: " + m + " N: " + n + " ");
	console.log("After Delete ");
	task.deleteNode(m, n);
	task.display();
}
// 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 LinkedList :
	#  Class constructors
	def __init__(self) :
		self.head = None
	
	#  Insert new node at the last
	def insert(self, value) :
		#  Create new node
		node = Node(value)
		if (self.head == None) :
			self.head = node
		else :
			temp = self.head
			#  find last node
			while (temp.next != None) :
				#  visit to next node
				temp = temp.next
			
			#  add node
			temp.next = node
		
	
	#  Display all Linked List elements
	def display(self) :
		if (self.head != None) :
			print("Linked List Element :", end = "")
			temp = self.head
			while (temp != None) :
				print(" ", temp.data, end = " ")
				temp = temp.next
			
		else :
			print("Empty Linked list")
		
	
	def deleteNode(self, m, n) :
		if (self.head == None) :
			print("Empty Linked List")
		elif (m <= 0 or n <= 0) :
			print("Invalid position")
		else :
			count = 0
			find = 0
			temp = self.head
			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
	task = LinkedList()
	task.insert(1)
	task.insert(2)
	task.insert(3)
	task.insert(4)
	task.insert(5)
	task.insert(6)
	task.insert(7)
	task.insert(8)
	task.insert(9)
	print("\nBefore Delete ")
	task.display()
	print("\n M: ", m ," N: ", n ," ")
	print("After Delete ")
	task.deleteNode(m, n)
	task.display()

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_reader :data, :next
	attr_accessor :data, :next
	def initialize(value) 
		self.data = value
		self.next = nil
	end

end

class LinkedList 
	# Define the accessor and reader of class LinkedList
	attr_reader :head
	attr_accessor :head
	#  Class constructors
	def initialize() 
		self.head = nil
	end

	#  Insert new node at the last
	def insert(value) 
		#  Create new node
		node = Node.new(value)
		if (self.head == nil) 
			self.head = node
		else
 
			temp = self.head
			#  find last node
			while (temp.next != nil) 
				#  visit to next node
				temp = temp.next
			end

			#  add node
			temp.next = node
		end

	end

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

		else
 
			print("Empty Linked list", "\n")
		end

	end

	def deleteNode(m, n) 
		if (self.head == nil) 
			print("Empty Linked List", "\n")
		elsif (m <= 0 || n <= 0) 
			print("Invalid position", "\n")
		else
 
			count = 0
			find = 0
			temp = self.head
			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
	task = LinkedList.new()
	task.insert(1)
	task.insert(2)
	task.insert(3)
	task.insert(4)
	task.insert(5)
	task.insert(6)
	task.insert(7)
	task.insert(8)
	task.insert(9)
	print("\nBefore Delete ", "\n")
	task.display()
	print("\n M: ", m ," N: ", n ," ", "\n")
	print("After Delete ", "\n")
	task.deleteNode(m, n)
	task.display()
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 LinkedList(var head: Node)
{
	// 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);
		if (head == null)
		{
			head = node;
		}
		else
		{
			var temp: Node = head;
			// find last node
			while (temp.next != null)
			{
				// visit to next node
				temp = temp.next;
			}
			// add node
			temp.next = node;
		}
	}
	// Display all Linked List elements
	def display(): Unit = {
		if (this.head != null)
		{
			print("Linked List Element :");
			var temp: Node = head;
			while (temp != null)
			{
				print("  " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			println("Empty Linked list");
		}
	}
	def deleteNode(m: Int, n: Int): Unit = {
		if (this.head == null)
		{
			println("Empty Linked List");
		}
		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 = 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;
		var task: LinkedList = new LinkedList();
		task.insert(1);
		task.insert(2);
		task.insert(3);
		task.insert(4);
		task.insert(5);
		task.insert(6);
		task.insert(7);
		task.insert(8);
		task.insert(9);
		println("\nBefore Delete ");
		task.display();
		println("\n M: " + m + " N: " + n + " ");
		println("After Delete ");
		task.deleteNode(m, n);
		task.display();
	}
}

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 LinkedList
{
	var head: Node? ;
	// Class constructors
	init()
	{
		self.head = nil;
	}
	// Insert new node at the last
	func insert(_ value: Int)
	{
		// Create new node
		let node: Node? = Node(value);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			var temp: Node? = self.head;
			// find last node
			while (temp!.next  != nil)
			{
				// visit to next node
				temp = temp!.next;
			}
			// add node
			temp!.next = node;
		}
	}
	// Display all Linked List elements
	func display()
	{
		if (self.head  != nil)
		{
			print("Linked List Element :", terminator: "");
			var temp: Node? = self.head;
			while (temp  != nil)
			{
				print("  "
					+ String(temp!.data), terminator: "");
				temp = temp!.next;
			}
		}
		else
		{
			print("Empty Linked list");
		}
	}
	func deleteNode(_ m: Int, _ n: Int)
	{
		if (self.head == nil)
		{
			print("Empty Linked List");
		}
		else if (m <= 0 || n <= 0)
		{
			print("Invalid position");
		}
		else
		{
			var count: Int = 0;
			var find: Int = 0;
			var temp: Node? = self.head;
			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;
		let task: LinkedList? = LinkedList();
		task!.insert(1);
		task!.insert(2);
		task!.insert(3);
		task!.insert(4);
		task!.insert(5);
		task!.insert(6);
		task!.insert(7);
		task!.insert(8);
		task!.insert(9);
		print("\nBefore Delete ");
		task!.display();
		print("\n M: "
			+ String(m) + " N: "
			+ String(n) + " ");
		print("After Delete ");
		task!.deleteNode(m, n);
		task!.display();
	}
}
LinkedList.main([String]());

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 LinkedList
{
	var head: Node ? ;
	// Class constructors
	constructor()
	{
		this.head = null;
	}
	// Insert new node at the last
	fun insert(value: Int): Unit
	{
		// Create new node
		val node: Node = Node(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			var temp: Node ? = this.head;
			// find last node
			while (temp?.next != null)
			{
				// visit to next node
				temp = temp.next;
			}
			// add node
			temp?.next = node;
		}
	}
	// Display all Linked List elements
	fun display(): Unit
	{
		if (this.head != null)
		{
			print("Linked List Element :");
			var temp: Node ? = this.head;
			while (temp != null)
			{
				print("  " + temp.data);
				temp = temp.next;
			}
		}
		else
		{
			println("Empty Linked list");
		}
	}
	fun deleteNode(m: Int, n: Int): Unit
	{
		if (this.head == null)
		{
			println("Empty Linked List");
		}
		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;
	val task: LinkedList = LinkedList();
	task.insert(1);
	task.insert(2);
	task.insert(3);
	task.insert(4);
	task.insert(5);
	task.insert(6);
	task.insert(7);
	task.insert(8);
	task.insert(9);
	println("\nBefore Delete ");
	task.display();
	println("\n M: " + m + " N: " + n + " ");
	println("After Delete ");
	task.deleteNode(m, n);
	task.display();
}

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.

New Comment