Challenge 10: Return the Nth Node From End

Returning the nth node from the start of a linked list is easy. Can you return the "nth" node from the end of a list?

Problem statement

In the FindNth(int n) function, a certain n is specified as an argument. You simply need to return the node, which is n spaces away from the end of the linked list.

Input

The input is a position n.

Output

The output is the value of the node n positions from the end. It returns -1 if n is out of bound.

Sample input

LinkedList = 22->18->60->78->47->39->99 and n = 3

Sample output

47

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.