Check if a pair with given product exists in Linked list | Java code to find the pair with given product ; Link list node ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Function to check if pair with given product exists in the list Takes head pointer of the linked list and product ; Check if pair exits ; Driver program to test above function ; Start with the empty list ; Use push ( ) to construct linked list ; function to print the result
import java . util . * ; class Solution { static final int MAX = 100000 ; 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 check_pair_product ( Node head , int prod ) { Vector < Integer > s = new Vector < Integer > ( ) ; Node p = head ; while ( p != null ) { int curr = p . data ; if ( ( prod % curr == 0 ) && ( s . contains ( prod / curr ) ) ) { System . out . print ( curr + " ▁ " + ( prod / curr ) ) ; return true ; } s . add ( p . data ) ; p = p . next ; } return false ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; head = push ( head , 12 ) ; head = push ( head , 1 ) ; head = push ( head , 18 ) ; head = push ( head , 47 ) ; head = push ( head , 16 ) ; head = push ( head , 12 ) ; head = push ( head , 14 ) ; boolean res = check_pair_product ( head , 24 ) ; if ( res == false ) System . out . println ( " NO ▁ PAIR ▁ EXIST " ) ; } }