Delete all prime nodes from a Singly Linked List
The problem at hand is to delete all prime nodes from a singly linked list. A singly linked list is a linear data structure where each element, known as a node, holds a value and a reference to the next node in the sequence. In this case, we are dealing with a singly linked list of integers. The task is to identify and remove all nodes that contain prime numbers from the linked list.
Problem Statement
Given a singly linked list of integers, we need to delete all nodes that contain prime numbers.
Example
Consider the following linked list:
7 > 1 > 8 > 2 > 4 > 23 > 37 > NULL
After deleting prime nodes, the list becomes:
1 > 8 > 4 > NULL
Idea to Solve the Problem
To solve this problem, we can iterate through the linked list while checking each node's value for primality. If a
prime node is found, we will remove it from the list. We can maintain three pointers: temp
,
hold
, and prev
. The temp
pointer will traverse through the list,
hold
will keep track of the prime node to be removed, and prev
will be used to adjust the
references after removing nodes.
Algorithm
 Initialize
temp
as the head of the linked list andhold
andprev
as null.  Iterate through the linked list using the
temp
pointer. If the value of
temp
node is prime: Set
hold
astemp
to mark the prime node for removal.
 Set
 Else:
 Set
prev
astemp
(since we need to keep track of the previous nonprime node).
 Set
 Move
temp
to the next node.
 If the value of
 If
hold
is not null: If
hold
is the head node: Update the head to skip the prime node (i.e., set
head
astemp
).
 Update the head to skip the prime node (i.e., set
 Else:
 Update the
next
reference of the previous nonprime node to skip the prime node (i.e., setprev.next
astemp
).
 Update the
 If
 Repeat steps 23 until the end of the linked list is reached.
 The linked list will now contain only the nonprime nodes.
Pseudocode
function deletePrimeNodes():
temp = head
hold = null
prev = null
while temp is not null:
if isPrime(temp.data):
hold = temp
else:
prev = temp
temp = temp.next
if hold is not null:
if hold is head:
head = temp
else:
prev.next = temp
hold = null
Code Solution

1) Delete all prime nodes from a singly linked list in java
2) Delete all prime nodes from a singly linked list in c++
3) Delete all prime nodes from a singly linked list in c
4) Delete all prime nodes from a singly linked list in golang
5) Delete all prime nodes from a singly linked list in kotlin
6) Delete all prime nodes from a singly linked list in swift
7) Delete all prime nodes from a singly linked list in scala
8) Delete all prime nodes from a singly linked list in ruby
9) Delete all prime nodes from a singly linked list in python
10) Delete all prime nodes from a singly linked list in typeScript
11) Delete all prime nodes from a singly linked list in node
12) Delete all prime nodes from a singly linked list in php
13) Delete all prime nodes from a singly linked list in vb.net
14) Delete all prime nodes from a singly linked list in c#
Time Complexity
The time complexity of the algorithm is determined by the traversal of the linked list. In the worst case, each node is visited once, resulting in a time complexity of O(n), where n is the number of nodes in the linked list.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment