class
Node {
int
data;
Node next;
Node(
int
data)
{
this
.data = data;
this
.next =
null
;
}
}
class
LinkedList {
static
Node push(Node head_ref,
int
new_data)
{
Node new_node =
new
Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return
head_ref;
}
}
class
Search {
static
boolean
findElement(Node head,
int
element)
{
if
(head ==
null
) {
return
false
;
}
else
if
(head.data == element) {
return
true
;
}
return
findElement(head.next, element);
}
}
class
PairSum {
static
boolean
check_pair_sum(Node head,
int
sum)
{
boolean
found =
false
;
while
(head !=
null
) {
found
= Search.findElement(head, sum - head.data);
if
(found ==
true
) {
System.out.println(head.data +
" and "
+ (sum - head.data));
return
found;
}
head = head.next;
}
return
found;
}
}
class
Main {
public
static
void
main(String[] args)
{
Node head =
null
;
head = LinkedList.push(head,
1
);
head = LinkedList.push(head,
4
);
head = LinkedList.push(head,
1
);
head = LinkedList.push(head,
12
);
head = LinkedList.push(head,
1
);
head = LinkedList.push(head,
18
);
head = LinkedList.push(head,
47
);
head = LinkedList.push(head,
16
);
head = LinkedList.push(head,
12
);
head = LinkedList.push(head,
14
);
head = LinkedList.push(head,
0
);
boolean
res = PairSum.check_pair_sum(head,
26
);
if
(res ==
false
)
System.out.println(
"No pair found"
);
}
}