class
GFG
{
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
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
minmaxPrimeNodes(Node head_ref)
{
int
minimum = Integer.MAX_VALUE;
int
maximum = Integer.MIN_VALUE;
Node ptr = head_ref;
while
(ptr !=
null
)
{
if
(isPrime(ptr.data))
{
minimum = Math.min(minimum, ptr.data);
maximum = Math.max(maximum, ptr.data);
}
ptr = ptr.next;
}
System.out.println(
"Minimum : "
+ minimum );
System.out.println(
"Maximum : "
+ maximum );
}
public
static
void
main(String args[])
{
Node head =
null
;
head = push(head,
17
);
head = push(head,
7
);
head = push(head,
6
);
head = push(head,
16
);
head = push(head,
15
);
minmaxPrimeNodes(head);
}
}