import
java.util.*;
class
GFG
{
static
int
count =
0
, data =
0
;
static
class
Node
{
int
data;
Node next;
}
static
Node getNode(
int
data)
{
Node newNode =
new
Node();
newNode.data = data;
newNode.next =
null
;
return
newNode;
}
static
void
findNthFromLast(Node head,
int
n,
Node nth_last)
{
if
(head ==
null
)
return
;
findNthFromLast(head.next, n, nth_last);
count = count +
1
;
if
(count == n)
{
data = head.data;
}
}
static
void
findNthFromLastUtil(Node head,
int
n)
{
Node nth_last =
new
Node();
findNthFromLast(head, n, nth_last);
if
(nth_last !=
null
)
System.out.println(
"Nth node from last is: "
+
data);
else
System.out.println(
"Node does not exists"
);
}
public
static
void
main(String args[])
{
Node head = getNode(
4
);
head.next = getNode(
2
);
head.next.next = getNode(
1
);
head.next.next.next = getNode(
5
);
head.next.next.next.next = getNode(
3
);
int
n =
2
;
findNthFromLastUtil(head, n);
}
}