Program to return nth node from the end in a linked list | faceprep

Program to return nth node from the end in a linked list | faceprep

The program to find the nth node from the end of a linked list is discussed here. Given a linked list, the task is to find the Nth node from the end of the list and return it.



Program to return nth node from the end of a linked list


This problem can be solved in two different ways.

Method 1: By finding the length of the linked list, the Nth node from the end can be found easily.

Method 2: By using two pointers, the Nth node from the end can be found.

For example, consider the linked list given below.

Linked List : 1 -> 2 -> 3 -> 4 -> 5

4th node from the end is 2

Algorithm to find the Nth node from the end (using the length of the linked list)

  • Input the number of nodes of the linked list.
  • Input all the nodes and create the linked list.
  • Input the Nth node to be returned from the end of the linked list.
  • Find the length of the linked list.
  • Return (length – N + 1)th node.


face prep pro ad bannerClick here to learn more about FACE Prep PRO


Program to return the Nth node from the end of a linked list using the length

@@coding::1@@


Time complexity: O(n)

Algorithm to find the Nth node from the end (using two pointers)

  • Input the number of nodes of the linked list.
  • Input all the nodes and create the linked list.
  • Input the Nth node to be returned from the end of the linked list.
  • Initialize both the reference pointer and the main pointer to the head node.
  • First, move the reference pointer to n nodes from the head node.
  • Now, move both the pointers one by one until the reference pointer reaches the end of the list.
  • Now, the main pointer will point to the nth node from the end.
  • Return the main pointer.


face prep pro ad bannerClick here to learn more about FACE Prep PRO


Program to find the Nth node from the end of the linked list using two pointers

@@coding::2@@


Recommended Programs


Program to return nth node from the end of a linked list

c