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”

Method 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.
Double pointer concept : First pointer is used to store the address of the variable and second pointer used to store the address of the first pointer. If we wish to change the value of a variable by a function, we pass pointer to it. And if we wish to change value of a pointer (i. e., it should start pointing to something else), we pass pointer to a pointer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void printNthFromLast( struct Node* head, int n)
{
int len = 0, i;
struct Node* temp = head;
while (temp != NULL) {
temp = temp->next;
len++;
}
if (len < n)
return ;
temp = head;
for (i = 1; i < len - n + 1; i++)
temp = temp->next;
cout << temp->data;
return ;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 35);
printNthFromLast(head, 4);
return 0;
}
|
Java
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void printNthFromLast( int n)
{
int len = 0 ;
Node temp = head;
while (temp != null ) {
temp = temp.next;
len++;
}
if (len < n)
return ;
temp = head;
for ( int i = 1 ; i < len - n + 1 ; i++)
temp = temp.next;
System.out.println(temp.data);
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push( 20 );
llist.push( 4 );
llist.push( 15 );
llist.push( 35 );
llist.printNthFromLast( 4 );
}
}
|
Python3
class Node:
def __init__( self , new_data):
self .data = new_data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printNthFromLast( self , n):
temp = self .head
length = 0
while temp is not None :
temp = temp. next
length + = 1
if n > length:
print ( 'Location is greater than the' +
' length of LinkedList' )
return
temp = self .head
for i in range ( 0 , length - n):
temp = temp. next
print (temp.data)
llist = LinkedList()
llist.push( 20 )
llist.push( 4 )
llist.push( 15 )
llist.push( 35 )
llist.printNthFromLast( 4 )
|
C#
using System;
public class LinkedList
{
public Node head;
public class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
void printNthFromLast( int n)
{
int len = 0;
Node temp = head;
while (temp != null )
{
temp = temp.next;
len++;
}
if (len < n)
return ;
temp = head;
for ( int i = 1; i < len - n + 1; i++)
temp = temp.next;
Console.WriteLine(temp.data);
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void Main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(4);
llist.push(15);
llist.push(35);
llist.printNthFromLast(4);
}
}
|
Following is a recursive C code for the same method. Thanks to Anuj Bansal for providing following code.
C
void printNthFromLast( struct Node* head, int n)
{
static int i = 0;
if (head == NULL)
return ;
printNthFromLast(head->next, n);
if (++i == n)
printf ( "%d" , head->data);
}
|
Java
static void printNthFromLast(Node head, int n)
{
static int i = 0 ;
if (head == null )
return ;
printNthFromLast(head.next, n);
if (++i == n)
System.out.print(head.data);
}
|
C#
static void printNthFromLast(Node head, int n)
{
static int i = 0;
if (head == null )
return ;
printNthFromLast(head.next, n);
if (++i == n)
Console.Write(head.data);
}
|
Time Complexity: O(n) where n is the length of linked list.
Method 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:

Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void printNthFromLast( struct Node *head, int n)
{
struct Node *main_ptr = head;
struct Node *ref_ptr = head;
int count = 0;
if (head != NULL)
{
while ( count < n )
{
if (ref_ptr == NULL)
{
printf ( "%d is greater than the no. of "
"nodes in list" , n);
return ;
}
ref_ptr = ref_ptr->next;
count++;
}
if (ref_ptr == NULL)
{
head = head->next;
if (head != NULL)
printf ( "Node no. %d from last is %d " , n, main_ptr->data);
}
else
{
while (ref_ptr != NULL)
{
main_ptr = main_ptr->next;
ref_ptr = ref_ptr->next;
}
printf ( "Node no. %d from last is %d " , n, main_ptr->data);
}
}
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 35);
printNthFromLast(head, 4);
}
|
Java
class LinkedList
{
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void printNthFromLast( int n)
{
Node main_ptr = head;
Node ref_ptr = head;
int count = 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 )
{
head = head.next;
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);
}
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push( 20 );
llist.push( 4 );
llist.push( 15 );
llist.push( 35 );
llist.printNthFromLast( 4 );
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printNthFromLast( self , n):
main_ptr = self .head
ref_ptr = self .head
count = 0
if ( self .head is not None ):
while (count < n ):
if (ref_ptr is None ):
print " % d is greater than the
no. pf nodes in list " % (n)
return
ref_ptr = ref_ptr. next
count + = 1
if (ref_ptr is None ):
self .head = self .head. next
if ( self .head is not None ):
print "Node no. % d from last is % d "
% (n, main_ptr.data)
else :
while (ref_ptr is not None ):
main_ptr = main_ptr. next
ref_ptr = ref_ptr. next
print "Node no. % d from last is % d "
% (n, main_ptr.data)
llist = LinkedList()
llist.push( 20 )
llist.push( 4 )
llist.push( 15 )
llist.push( 35 )
llist.printNthFromLast( 4 )
|
C#
using System;
public class LinkedList
{
Node head;
public class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
void printNthFromLast( int n)
{
Node main_ptr = head;
Node ref_ptr = head;
int count = 0;
if (head != null )
{
while (count < n)
{
if (ref_ptr == null )
{
Console.WriteLine(n + " is greater than the no "
+ " of nodes in the list" );
return ;
}
ref_ptr = ref_ptr.next;
count++;
}
if (ref_ptr == null )
{
head = head.next;
if (head != null )
Console.WriteLine( "Node no. " +
n + " from last is " +
main_ptr.data);
}
else
{
while (ref_ptr != null )
{
main_ptr = main_ptr.next;
ref_ptr = ref_ptr.next;
}
Console.WriteLine( "Node no. " +
n + " from last is " +
main_ptr.data);
}
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void Main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(4);
llist.push(15);
llist.push(35);
llist.printNthFromLast(4);
}
}
|
OutputNode no. 4 from last is 35
Time Complexity: O(n) where n is the length of linked list.
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.