Home » Data Structure & Algorithms » Nth Node from end of LinkedList

Nth Node from end of LinkedList


Question :
WAP to find the nth  node from end of the linkedlist in 0(N) time complexity.

Solution :
The problem is solved in recursive way.

Algorithm : 
Step 1 : If head is null, then return -1.
Step 2 : Take two pointer fast and slow.
Step 3 : Move the fast pointer  by 1 position till the counter reaches value n.
Step 4 : Move both the slow and the fast pointer  by 1 position till fast pointer reaches end.
Step 4 : The slow pointer points  the nth node from end of the LinkedList.

Program :

Output :


Leave a comment

Your email address will not be published. Required fields are marked *

sixteen − six =