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