# Recursive Approach to find nth node from the end in the linked list

Find the nth node from the end in the given linked list using a recursive approach.

Examples:

```Input : list: 4->2->1->5->3
n = 2
Output : 5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm:

```findNthFromLast(head, n, count, nth_last)
return

count = count + 1
if count == n then

Initialize nth_last = NULL
Initialize count = 0

if nth_last != NULL then
print nth_last->data
else
print "Node does not exists"
```

Note: Parameters count and nth_last will be pointer variables in findNthFromLast().

## C++

 `// C++ implementation to recursively find the nth node from ` `// the last of the linked list ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// structure of a node of a linked list ` `struct` `Node { ` `    ``int` `data; ` `    ``Node* next; ` `}; ` ` `  `// function to get a new node ` `Node* getNode(``int` `data) ` `{ ` `    ``// allocate space ` `    ``Node* newNode = ``new` `Node; ` ` `  `    ``// put in data ` `    ``newNode->data = data; ` `    ``newNode->next = NULL; ` `    ``return` `newNode; ` `} ` ` `  `// funnction to recursively find the nth node from ` `// the last of the linked list ` `void` `findNthFromLast(Node* head, ``int` `n, ``int``* count, ` `                     ``Node** nth_last) ` `{ ` `    ``// if list is empty ` `    ``if` `(!head) ` `        ``return``; ` ` `  `    ``// recursive call ` `    ``findNthFromLast(head->next, n, count, nth_last); ` ` `  `    ``// increment count ` `    ``*count = *count + 1; ` ` `  `    ``// if true, then head is the nth node from the last ` `    ``if` `(*count == n) ` `        ``*nth_last = head; ` `} ` ` `  `// utility function to find the nth node from ` `// the last of the linked list ` `void` `findNthFromLastUtil(Node* head, ``int` `n) ` `{ ` `    ``// Initialize ` `    ``Node* nth_last = NULL; ` `    ``int` `count = 0; ` ` `  `    ``// find nth node from the last ` `    ``findNthFromLast(head, n, &count, &nth_last); ` ` `  `    ``// if node exists, then print it ` `    ``if` `(nth_last != NULL) ` `        ``cout << ``"Nth node from last is: "` `             ``<< nth_last->data; ` `    ``else` `        ``cout << ``"Node does not exists"``; ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``// linked list: 4->2->1->5->3 ` `    ``Node* head = getNode(4); ` `    ``head->next = getNode(2); ` `    ``head->next->next = getNode(1); ` `    ``head->next->next->next = getNode(5); ` `    ``head->next->next->next->next = getNode(3); ` ` `  `    ``int` `n = 2; ` ` `  `    ``findNthFromLastUtil(head, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to recursively  ` `// find the nth node from the last ` `// of the linked list  ` `import` `java.util.*; ` `class` `GFG ` `{ ` `static` `int` `count = ``0``, data = ``0``; ` ` `  `// a node of a linked list  ` `static` `class` `Node  ` `{  ` `    ``int` `data;  ` `    ``Node next;  ` `} ` ` `  `// function to get a new node  ` `static` `Node getNode(``int` `data)  ` `{  ` `    ``// allocate space  ` `    ``Node newNode = ``new` `Node();  ` ` `  `    ``// put in data  ` `    ``newNode.data = data;  ` `    ``newNode.next = ``null``;  ` `    ``return` `newNode;  ` `}  ` ` `  `// funnction to recursively  ` `// find the nth node from  ` `// the last of the linked list  ` `static` `void` `findNthFromLast(Node head, ``int` `n,  ` `                            ``Node nth_last)  ` `{  ` `    ``// if list is empty  ` `    ``if` `(head == ``null``)  ` `        ``return``;  ` ` `  `    ``// recursive call  ` `    ``findNthFromLast(head.next, n, nth_last);  ` ` `  `    ``// increment count  ` `    ``count = count + ``1``;  ` `     `  `    ``// if true, then head is the ` `    ``// nth node from the last  ` `    ``if` `(count == n)  ` `    ``{ ` `        ``data = head.data;  ` `    ``} ` `}  ` ` `  `// utility function to find  ` `// the nth node from the last ` `// of the linked list  ` `static` `void` `findNthFromLastUtil(Node head, ``int` `n)  ` `{  ` `    ``// Initialize  ` `    ``Node nth_last = ``new` `Node();  ` `    ``count = ``0``;  ` ` `  `    ``// find nth node from the last  ` `    ``findNthFromLast(head, n, nth_last);  ` ` `  `    ``// if node exists, then print it  ` `    ``if` `(nth_last != ``null``)  ` `        ``System.out.println(``"Nth node from last is: "` `+ ` `                                                ``data);  ` `    ``else` `        ``System.out.println(``"Node does not exists"``);  ` `}  ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{  ` `    ``// linked list: 4.2.1.5.3  ` `    ``Node head = getNode(``4``);  ` `    ``head.next = getNode(``2``);  ` `    ``head.next.next = getNode(``1``);  ` `    ``head.next.next.next = getNode(``5``);  ` `    ``head.next.next.next.next = getNode(``3``);  ` ` `  `    ``int` `n = ``2``;  ` ` `  `    ``findNthFromLastUtil(head, n);  ` `}  ` `} ` ` `  `// This code is contributed  ` `// by Arnab Kundu `

## C#

 `// C# implementation to recursively  ` `// find the nth node from the last ` `// of the linked list  ` `using` `System; ` ` `  `public` `class` `GFG ` `{ ` `    ``static` `int` `count = 0, data = 0; ` ` `  `    ``// a node of a linked list  ` `    ``class` `Node  ` `    ``{  ` `        ``public` `int` `data;  ` `        ``public` `Node next;  ` `    ``} ` ` `  `// function to get a new node  ` `static` `Node getNode(``int` `data)  ` `{  ` `    ``// allocate space  ` `    ``Node newNode = ``new` `Node();  ` ` `  `    ``// put in data  ` `    ``newNode.data = data;  ` `    ``newNode.next = ``null``;  ` `    ``return` `newNode;  ` `}  ` ` `  `// funnction to recursively  ` `// find the nth node from  ` `// the last of the linked list  ` `static` `void` `findNthFromLast(Node head, ``int` `n,  ` `                            ``Node nth_last)  ` `{  ` `    ``// if list is empty  ` `    ``if` `(head == ``null``)  ` `        ``return``;  ` ` `  `    ``// recursive call  ` `    ``findNthFromLast(head.next, n, nth_last);  ` ` `  `    ``// increment count  ` `    ``count = count + 1;  ` `     `  `    ``// if true, then head is the ` `    ``// nth node from the last  ` `    ``if` `(count == n)  ` `    ``{ ` `        ``data = head.data;  ` `    ``} ` `}  ` ` `  `// utility function to find  ` `// the nth node from the last ` `// of the linked list  ` `static` `void` `findNthFromLastUtil(Node head, ``int` `n)  ` `{  ` `    ``// Initialize  ` `    ``Node nth_last = ``new` `Node();  ` `    ``count = 0;  ` ` `  `    ``// find nth node from the last  ` `    ``findNthFromLast(head, n, nth_last);  ` ` `  `    ``// if node exists, then print it  ` `    ``if` `(nth_last != ``null``)  ` `        ``Console.WriteLine(``"Nth node from last is: "` `+ ` `                                                ``data);  ` `    ``else` `        ``Console.WriteLine(``"Node does not exists"``);  ` `}  ` ` `  `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{  ` `    ``// linked list: 4.2.1.5.3  ` `    ``Node head = getNode(4);  ` `    ``head.next = getNode(2);  ` `    ``head.next.next = getNode(1);  ` `    ``head.next.next.next = getNode(5);  ` `    ``head.next.next.next.next = getNode(3);  ` ` `  `    ``int` `n = 2;  ` ` `  `    ``findNthFromLastUtil(head, n);  ` `}  ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Nth node from last is: 5
```

Time Complexity: O(n), where ‘n’ is the number of nodes in the linked list.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : andrew1234, Rajput-Ji

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.