C++ Program For Printing Nth Node From The End Of A Linked List
Last Updated :
24 Jan, 2023
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):
- Calculate the length of Linked List. Let the length be len.
- 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
// C++ program to find n'th node from
// end
#include <bits/stdc++.h>
using namespace std;
// Link list node
struct Node
{
int data;
struct Node* next;
};
/* Function to get the nth node from
the last of a linked list*/
void printNthFromLast(struct Node* head,
int n)
{
int len = 0, i;
struct Node* temp = head;
// count the number of nodes in
// Linked List
while (temp != NULL)
{
temp = temp->next;
len++;
}
// check if value of n is not
// more than length of the
// linked list
if (len < n)
return;
temp = head;
// get the (len-n+1)th node from
// the beginning
for (i = 1; i < len - n + 1; i++)
temp = temp->next;
cout << temp->data;
return;
}
void push(struct Node** head_ref,
int new_data)
{
// Allocate node
struct Node* new_node = new Node();
// Put in the data
new_node->data = new_data;
// Link the old list of the
// new node
new_node->next = (*head_ref);
// Move the head to point to
// the new node
(*head_ref) = new_node;
}
// Driver Code
int main()
{
// Start with the empty list
struct Node* head = NULL;
// Create linked 35->15->4->20
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 35);
printNthFromLast(head, 4);
return 0;
}
Output:
35
C++
void printNthFromLast(struct Node* head, int n)
{
int i = 0;
if (head == NULL)
return;
printNthFromLast(head->next, n);
if (++i == n)
cout<<head->data;
}
Time Complexity: O(n) where n is the length of linked list.
Space Complexity: O(1) because using constant space for pointer manipulation
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++
// Simple C++ program to
// find n'th node from end
#include<bits/stdc++.h>
using namespace std;
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
/* Function to get the nth node
from the last of a linked list*/
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++;
} /* End of while*/
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);
}
}
}
// Function to push
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node();
/* put in the data */
new_node->data = new_data;
/* link the old list of the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 35);
printNthFromLast(head, 4);
}
OutputNode no. 4 from last is 35
Time Complexity: O(n) where n is the length of linked list.
Space complexity: O(1) since using constant space
Please refer complete article on Program for n'th node from the end of a Linked List for more details!
Similar Reads
Program for Nth node from the end of a Linked List Given a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1.Examples:Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list.Input: 35 ->
14 min read
Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the
8 min read
Write a function to get Nth node in a Linked List Given a LinkedList and an index (1-based). The task is to find the data value stored in the node at that kth position. If no such node exists whose index is k then return -1.Example:Â Input: 1->10->30->14, index = 2Output: 10Explanation: The node value at index 2 is 10 Input: 1->32->12
11 min read
Swap Kth node from beginning with Kth node from end in a Linked List Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details like Name, RollNo, Address, ..etc
15+ min read
Print the last k nodes of the linked list in reverse order | Iterative Approaches Given a linked list containing N nodes and a positive integer K where K should be less than or equal to N. The task is to print the last K nodes of the list in reverse order. Examples: Input : list: 1->2->3->4->5, K = 2 Output : 5 4 Input : list: 3->10->6->9->12->2->8,
15+ min read
Traversal of Singly Linked List Traversal of Singly Linked List is one of the fundamental operations, where we traverse or visit each node of the linked list. In this article, we will cover how to traverse all the nodes of a singly linked list along with its implementation. Examples:Input: 1->2->3->4->5->nullOutput:
11 min read