Delete all the nodes from the list which are divisible by any given number K

Given a Linked List and a key K. The task is to write a program to delete all the nodes from the list that are divisible by K.

Examples:

Input : 12->15->9->11->5->6->7
        K = 3
Output :  11 -> 5 -> 7

Input :13->4->16->9->22->45->5->16->6
        K = 4
Output : 13 -> 9 -> 22 -> 45 -> 5 -> 6

The approach to solving this problem is similar to deleting all nodes from the list which are lesser than the given key.



There are two possible cases:

  1. Head node holds a value divisible by K: First check for all occurrences at head node which are divisible by K, delete them and change the head node appropriately.
  2. Some intermediate node holds value divisible by k: Start traversing from head and check if current node’s value is divisible by K. If yes then delete that node and move forward in the list.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to delete all the nodes from the list
// that are divisible by  the specified value K
#include <bits/stdc++.h>
  
using namespace std;
  
// Tree Node
struct Node {
    int data;
    Node* next;
};
  
// Function to create a new node
Node* getNode(int data)
{
    Node* newNode = new Node;
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
  
// Function to delete all the nodes from the list
// that divisible by the specified value K
void deleteDivisibleNodes(Node** head_ref, int K)
{
    Node *temp = *head_ref, *prev;
  
    // If head node itself holds the value divisible by K
    while (temp != NULL && temp->data % K == 0) {
        *head_ref = temp->next; // Changed head
        free(temp); // free old head
        temp = *head_ref; // Change temp
    }
  
    // Delete occurrences other than head
    while (temp != NULL) {
  
        // Search for the node to be deleted,
        // keep track of the previous node as we
        // need to change 'prev->next'
        while (temp != NULL && temp->data % K != 0) {
            prev = temp;
            temp = temp->next;
        }
  
        // If required value node was not present
        // in linked list
        if (temp == NULL)
            return;
  
        // Unlink the node from linked list
        prev->next = temp->next;
  
        delete temp; // Free memory
  
        // Update Temp for next iteration of
        // outer loop
        temp = prev->next;
    }
}
  
// function to a print a linked list
void printList(Node* head)
{
    while (head) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
// Driver code
int main()
{
    // Create list: 12->15->9->11->5->6->7
    Node* head = getNode(12);
    head->next = getNode(15);
    head->next->next = getNode(9);
    head->next->next->next = getNode(11);
    head->next->next->next->next = getNode(5);
    head->next->next->next->next->next = getNode(6);
    head->next->next->next->next->next->next = getNode(7);
    int K = 3;
  
    cout << "Initial List: ";
    printList(head);
  
    deleteDivisibleNodes(&head, K);
  
    cout << "\nFinal List: ";
    printList(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to delete all the nodes from the list
// that are divisible by the specified value K
import java.util.*;
  
class GFG
{
  
// Tree Node
static class Node
{
    int data;
    Node next;
};
  
// Function to create a new node
static Node getNode(int data)
{
    Node newNode = new Node();
    newNode.data = data;
    newNode.next = null;
    return newNode;
}
  
// Function to delete all the nodes from the list
// that divisible by the specified value K
static Node deleteDivisibleNodes(Node head_ref, int K)
{
    Node temp = head_ref, prev = null;
  
    // If head node itself holds the value divisible by K
    while (temp != null && temp.data % K == 0
    {
        head_ref = temp.next; // Changed head
        //free(temp); // free old head
        temp = head_ref; // Change temp
    }
  
    // Delete occurrences other than head
    while (temp != null
    {
  
        // Search for the node to be deleted,
        // keep track of the previous node as we
        // need to change 'prev.next'
        while (temp != null && temp.data % K != 0
        {
            prev = temp;
            temp = temp.next;
        }
  
        // If required value node was not present
        // in linked list
        if (temp == null)
            return head_ref;
  
        // Unlink the node from linked list
        prev.next = temp.next;
  
        //delete temp; // Free memory
  
        // Update Temp for next iteration of
        // outer loop
        temp = prev.next;
    }
    return null;
}
  
// function to a print a linked list
static void printList(Node head)
{
    while (head != null)
    {
        System.out.print(head.data + " ");
        head = head.next;
    }
}
  
// Driver code
public static void main(String[] args)
{
    // Create list: 12->15->9->11->5->6->7
    Node head = getNode(12);
    head.next = getNode(15);
    head.next.next = getNode(9);
    head.next.next.next = getNode(11);
    head.next.next.next.next = getNode(5);
    head.next.next.next.next.next = getNode(6);
    head.next.next.next.next.next.next = getNode(7);
    int K = 3;
  
    System.out.print("Initial List: ");
    printList(head);
  
    head = deleteDivisibleNodes(head, K);
  
    System.out.print("\nFinal List: ");
    printList(head);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to delete all the nodes 
// from the list that are divisible 
// by the specified value K
using System;
  
class GFG
{
  
// Tree Node
public class Node
{
    public int data;
    public Node next;
};
  
// Function to create a new node
static Node getNode(int data)
{
    Node newNode = new Node();
    newNode.data = data;
    newNode.next = null;
    return newNode;
}
  
// Function to delete all the nodes
// from the list that divisible by
// the specified value K
static Node deleteDivisibleNodes(Node head_ref, 
                                         int K)
{
    Node temp = head_ref, prev = null;
  
    // If head node itself holds
    // the value divisible by K
    while (temp != null && 
           temp.data % K == 0) 
    {
        head_ref = temp.next; // Changed head
        //free(temp); // free old head
        temp = head_ref; // Change temp
    }
  
    // Delete occurrences other than head
    while (temp != null
    {
  
        // Search for the node to be deleted,
        // keep track of the previous node as we
        // need to change 'prev.next'
        while (temp != null &&
               temp.data % K != 0) 
        {
            prev = temp;
            temp = temp.next;
        }
  
        // If required value node 
        // was not present in linked list
        if (temp == null)
            return head_ref;
  
        // Unlink the node from linked list
        prev.next = temp.next;
  
        //delete temp; // Free memory
  
        // Update Temp for next iteration of
        // outer loop
        temp = prev.next;
    }
    return null;
}
  
// function to a print a linked list
static void printList(Node head)
{
    while (head != null)
    {
        Console.Write(head.data + " ");
        head = head.next;
    }
}
  
// Driver code
public static void Main(String[] args)
{
    // Create list: 12->15->9->11->5->6->7
    Node head = getNode(12);
    head.next = getNode(15);
    head.next.next = getNode(9);
    head.next.next.next = getNode(11);
    head.next.next.next.next = getNode(5);
    head.next.next.next.next.next = getNode(6);
    head.next.next.next.next.next.next = getNode(7);
    int K = 3;
  
    Console.Write("Initial List: ");
    printList(head);
  
    head = deleteDivisibleNodes(head, K);
  
    Console.Write("\nFinal List: ");
    printList(head);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

Initial List: 12 15 9 11 5 6 7 
Final List: 11 5 7



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992