Delete smaller nodes in linked list program in Java

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]]

Here given code implementation process.

    1] Delete n nodes after m nodes of a linked list in c 2] Delete n nodes after m nodes of a linked list in java 3] Delete n nodes after m nodes of a linked list in c++ 4] Delete n nodes after m nodes of a linked list in go 5] Delete n nodes after m nodes of a linked list in c# 6] Delete n nodes after m nodes of a linked list in vb.net 7] Delete n nodes after m nodes of a linked list in php 8] Delete n nodes after m nodes of a linked list in python 9] Delete n nodes after m nodes of a linked list in ruby 10] Delete n nodes after m nodes of a linked list in scala 11] Delete n nodes after m nodes of a linked list in swift 12] Delete n nodes after m nodes of a linked list in kotlin 13] Delete n nodes after m nodes of a linked list in node 14] Delete n nodes after m nodes of a linked list in typeScript

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
  • C++
  • Go
  • C
  • C#
  • Php
  • Node Js
  • Python
  • Ruby
  • Scala
  • Swift 4
  • Kotlin
// 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 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 head; while [temp != nullptr] { cout next; } } else { cout 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 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

Chủ Đề