The code provided is an implementation of a skipMdeleteN function that skips M nodes and then deletes N nodes in a linked list. The linked list is represented using a simple Node class with an integer data field and a next pointer.
The skipMdeleteN function takes three parameters - head (the head of the linked list), M (the number of nodes to skip), and N (the number of nodes to delete). It iterates through the linked list, skipping M nodes, and then deleting N nodes. This is done by updating the next pointers of the previous node and skipping N nodes in the linked list.
The main function creates a linked list with integer values from 1 to 10 using the push function. It then prints the original linked list, calls the skipMdeleteN function with M=2 and N=2, and prints the linked list after deletion.
Note: The implementation assumes 1-based indexing for M and N, meaning the first node is skipped and deleted. If you want to use 0-based indexing, you need to adjust the implementation accordingly. Also, the implementation does not handle cases where M or N are larger than the size of the linked list, so make sure to handle such cases if needed.
import java.util.*; class DeleteN_Nodes { static class Node { int data; Node next; }; static Node push( Node head_ref, int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = (head_ref); (head_ref) = new_node; return head_ref; } static void printList( Node head) { Node temp = head; while (temp != null) { System.out.printf("%d ", temp.data); temp = temp.next; } System.out.printf("\n"); } static void skipMdeleteN( Node head, int M, int N) { Node curr = head, t; int count; while (curr!=null) { for (count = 1; count < M && curr != null; count++) curr = curr.next; if (curr == null) return; t = curr.next; for (count = 1; count <= N && t != null; count++) { Node temp = t; t = t.next; } curr.next = t; curr = t; } } public static void main(String args[]) { Node head = null; int M=2, N=2; head=push(head, 10); head=push(head, 9); head=push(head, 8); head=push(head, 7); head=push(head, 6); head=push(head, 5); head=push(head, 4); head=push(head, 3); head=push(head, 2); head=push(head, 1); System.out.println("M : "+ M); System.out.println("N : "+ N); System.out.print("Given LinkedList is : "); printList(head); skipMdeleteN(head, M, N); System.out.printf("LinkedList After Deletion is : "); printList(head); } }
M : 2 N : 2 Given LinkedList is : 1 2 3 4 5 6 7 8 9 10 LinkedList After Deletion is : 1 2 5 6 9 10
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions