Given a Linked List and a number n, write a function that returns the value at the n’th node from the end of the Linked List.
For example, if the input is below list and n = 3, then output is “B”
Way 1 (Use length of linked list)
1) Calculate the length of Linked List. Let the length be len.
2) Print the (len – n + 1)th node from the beginning of the Linked List.
| classLinkedList {
     Node head; 
     
     classNode {
         intdata;
         Node next;
         Node(intd)
         {
             data = d;
             next = null;
         }
     }
     
        
     voidprintNthFromLast(intn)
     {
         intlen = 0;
         Node temp = head;
         
         while(temp != null) {
             temp = temp.next;
             len++;
         }
         
         
         if(len < n)
             return;
         temp = head;
         
         for(inti = 1; i < len - n + 1; i++)
             temp = temp.next;
         System.out.println(temp.data);
     }
     
     publicvoidpush(intnew_data)
     {
         
                   
         Node new_node = newNode(new_data);
         
         new_node.next = head;
         
         head = new_node;
     }
     
     publicstaticvoidmain(String[] args)
     {
         LinkedList llist = newLinkedList();
         llist.push(20);
         llist.push(4);
         llist.push(15);
         llist.push(35);
         llist.printNthFromLast(4);
     }
 } 
 | 
Output
Following is a recursive C code for the same method.
| staticvoidprintNthFromLast(Node head, intn)
 {
 staticinti = 0;
 if(head == null)
 return;
 printNthFromLast(head.next, n);
 if(++i == n)
 System.out.print(head.data);
 }
 | 
Time Complexity: O(n) where n is the length of linked list.
Way 2 (Use two pointers) 
Maintain two pointers – reference pointer and main pointer. Initialize both reference and main pointers to head. First, move the reference pointer to n nodes from head. Now move both pointers one by one until the reference pointer reaches the end. Now the main pointer will point to nth node from the end. Return the main pointer.
Below image is a dry run of the above approach:

| classLinkedList
 {
     Node head; 
     
     classNode {
         intdata;
         Node next;
         Node(intd)
         {
             data = d;
             next = null;
         }
     }
     
       
     voidprintNthFromLast(intn)
     {
         Node main_ptr = head;
         Node ref_ptr = head;
         intcount = 0;
         if(head != null)
         {
             while(count < n)
             {
                 if(ref_ptr == null)
                 {
                     System.out.println(n
                      + " is greater than the no "
                        + " of nodes in the list");
                     return;
                 }
                 ref_ptr = ref_ptr.next;
                 count++;
             }
             if(ref_ptr == null)
             {
              
               if(head != null)
                 System.out.println("Node no. "+ n +
                                    " from last is "+
                                       head.data);
             }
             else
             {
                   
               while(ref_ptr != null)
               {
                   main_ptr = main_ptr.next;
                   ref_ptr = ref_ptr.next;
               }
               System.out.println("Node no. "+ n +
                                 " from last is "+
                                   main_ptr.data);
             }
         }
     }
     
     publicvoidpush(intnew_data)
     {
         
                   
         Node new_node = newNode(new_data);
         
         new_node.next = head;
         
         head = new_node;
     }
     
     publicstaticvoidmain(String[] args)
     {
         LinkedList llist = newLinkedList();
         llist.push(20);
         llist.push(4);
         llist.push(15);
         llist.push(35);
         llist.printNthFromLast(4);
     }
 }
 | 
Output
Node no. 4 from last is 35