Challenge: Return the Nth node from End

Returning the Nth node from the start of a linked list is easy. Can you return 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 #

A position n.

Output #

The value of the node n positions from the end. 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.