import
java.util.*;
class
Node {
int
data;
Node next;
}
class
GFG {
static
void
push(Node head_ref[],
int
new_data) {
Node new_node =
new
Node();
new_node.data = new_data;
new_node.next = head_ref[
0
];
head_ref[
0
] = new_node;
}
static
boolean
isPrime(
int
n) {
if
(n <=
1
)
return
false
;
if
(n <=
3
)
return
true
;
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
;
}
static
void
deleteNode(Node head_ref[], Node del) {
if
(head_ref[
0
] ==
null
|| del ==
null
)
return
;
if
(head_ref[
0
] == del) {
head_ref[
0
] = del.next;
return
;
}
Node temp = head_ref[
0
];
while
(temp.next != del) {
temp = temp.next;
}
temp.next = del.next;
del =
null
;
}
static
void
deletePrimeNodes(Node head_ref[]) {
Node ptr = head_ref[
0
];
Node next;
while
(ptr !=
null
) {
next = ptr.next;
if
(isPrime(ptr.data))
deleteNode(head_ref, ptr);
ptr = next;
}
}
static
void
printList(Node head) {
while
(head !=
null
) {
System.out.print(head.data +
" "
);
head = head.next;
}
}
public
static
void
main(String[] args) {
Node[] head_ref =
new
Node[
1
];
head_ref[
0
] =
null
;
push(head_ref,
17
);
push(head_ref,
7
);
push(head_ref,
6
);
push(head_ref,
16
);
push(head_ref,
5
);
System.out.print(
"Original List: "
);
printList(head_ref[
0
]);
deletePrimeNodes(head_ref);
System.out.print(
"\nModified List: "
);
printList(head_ref[
0
]);
}
}