Delete all Prime Nodes from a Singly Linked List

Given a singly linked list containing N nodes, the task is to delete all nodes from the list which are prime.

Examples:

Input : List = 15 -> 16 -> 6 -> 7 -> 17
Output : Final List = 15 -> 16 -> 6

Input : List = 15 -> 3 -> 4 -> 2 -> 9
Output :Final List = 15 ->4 -> 9



Approach: The idea is to traverse the nodes of the singly linked list one by one and get the pointer of the nodes which are prime. Delete those nodes by following the approach used in the post: Delete a node from Linked List.

Below is the implementation of above idea:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to delete all
// prime nodes from the singly
// linked list
#include <bits/stdc++.h>
  
using namespace std;
  
// Node of the singly linked list
struct Node {
    int data;
    Node* next;
};
  
// function to insert a node at the beginning
// of the singly Linked List
void push(Node** head_ref, int new_data)
{
    // allocate node
    Node* new_node = (Node*)malloc(sizeof(struct Node));
  
    // put in the data
    new_node->data = new_data;
  
    // link the old list off the new node
    new_node->next = (*head_ref);
  
    // move the head to point to the new node
    (*head_ref) = new_node;
}
  
// Function to check if a number is prime
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
}
  
// function to delete a node in a singly Linked List.
// head_ref --> pointer to head node pointer.
// del --> pointer to node to be deleted
void deleteNode(Node** head_ref, Node* del)
{
    // base case
    struct Node* temp = *head_ref;
    if (*head_ref == NULL || del == NULL)
        return;
  
    // If node to be deleted is head node
    if (*head_ref == del)
        *head_ref = del->next;
    // travers list till not found
    // delete node
    while (temp->next != del) {
        temp = temp->next;
    }
    // copy address of node
    temp->next = del->next;
    // Finally, free the memory occupied by del
    free(del);
  
    return;
}
  
// function to delete all prime nodes
// from the singly linked list
void deletePrimeNodes(Node** head_ref)
{
    Node* ptr = *head_ref;
    Node* next;
  
    while (ptr != NULL) {
        next = ptr->next;
        // if true, delete node 'ptr'
        if (isPrime(ptr->data))
            deleteNode(head_ref, ptr);
        ptr = next;
    }
}
  
// function to print nodes in a
// given singly linked list
void printList(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
// Driver program
int main()
{
    // start with the empty list
    Node* head = NULL;
  
    // create the linked list
    // 15 -> 16 -> 7 -> 6 -> 17
    push(&head, 17);
    push(&head, 7);
    push(&head, 6);
    push(&head, 16);
    push(&head, 15);
  
    cout << "Original List: ";
    printList(head);
  
    deletePrimeNodes(&head);
  
    cout << "\nModified List: ";
    printList(head);
}

chevron_right


Output:

Original List: 15 16 6 7 17 
Modified List: 15 16 6

Time Complexity: O(N), where N is the total number of nodes.



My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

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.