Python Program To Delete N Nodes After M Nodes Of A Linked List Last Updated : 18 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples: Input: M = 2, N = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked List: 1->2->5->6 Input: M = 3, N = 2 Linked List: 1->2->3->4->5->6->7->8->9->10 Output: Linked List: 1->2->3->6->7->8 Input: M = 1, N = 1 Linked List: 1->2->3->4->5->6->7->8->9->10 Output: Linked List: 1->3->5->7->9Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. The main part of the problem is to maintain proper links between nodes, make sure that all corner cases are handled. Following is C implementation of function skipMdeleteN() that skips M nodes and delete N nodes till end of list. It is assumed that M cannot be 0. Python # Python program to delete M nodes # after N nodes # Node class class Node: # Constructor to initialize # the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None # Function to insert a new node # at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Utility function to print the # linked LinkedList def printList(self): temp = self.head while(temp): print temp.data, temp = temp.next def skipMdeleteN(self, M, N): curr = self.head # The main loop that traverses # through the whole list while(curr): # Skip M nodes for count in range(1, M): if curr is None: return curr = curr.next if curr is None : return # Start from next node and delete # N nodes t = curr.next for count in range(1, N+1): if t is None: break t = t.next # Link the previous list with # remaining nodes curr.next = t # Set Current pointer for next # iteration curr = t # Driver code # Create following linked list # 1->2->3->4->5->6->7->8->9->10 llist = LinkedList() M = 2 N = 3 llist.push(10) llist.push(9) llist.push(8) llist.push(7) llist.push(6) llist.push(5) llist.push(4) llist.push(3) llist.push(2) llist.push(1) print "M = %d, N = %d Given Linked List is:" %(M, N) llist.printList() print llist.skipMdeleteN(M, N) print "Linked list after deletion is" llist.printList() # This code is contributed by Nikhil Kumar Singh(nickzuck_007) Output: M = 2, N = 3 Given Linked list is : 1 2 3 4 5 6 7 8 9 10 Linked list after deletion is : 1 2 6 7 Time Complexity:O(n) where n is number of nodes in linked list. Auxiliary Space: O(1) Please refer complete article on Delete N nodes after M nodes of a linked list for more details! Comment More infoAdvertise with us Next Article Python Program for Deleting a Node in a Linked List K kartik Follow Improve Article Tags : Linked List Python Programs DSA Linked Lists Microsoft Amazon Python-DSA +3 More Practice Tags : AmazonMicrosoftLinked List Similar Reads Python Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read Python Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g 4 min read Python Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read Python Program for Deleting a Node in a Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Python Program For Making Middle Node Head In A Linked List Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5 Input: 1 2 3 4 5 6 Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves 3 min read Python Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2-> 4 min read Like