Recursive Approach to find nth node from the end in the linked list
Last Updated :
31 Jul, 2022
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
Algorithm:
findNthFromLast(head, n, count, nth_last)
if head == NULL then
return
findNthFromLast(head->next, n, count, nth_last)
count = count + 1
if count == n then
nth_last = head
findNthFromLastUtil(head, n)
Initialize nth_last = NULL
Initialize count = 0
findNthFromLast(head, n, &count, &nth_last)
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 <bits/stdc++.h>
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;
}
// function 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;
}
// function 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();
// 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
Python
# Python implementation to recursively
# find the nth node from the last
# of the linked list
count = 0
data = 0
# a node of a linked list
class Node(object):
def __init__(self, d):
self.data = d
self.next = None
# function to get a new node
def getNode(data):
# allocate space
newNode = Node(0)
# put in data
newNode.data = data
newNode.next = None
return newNode
# function to recursively
# find the nth node from
# the last of the linked list
def findNthFromLast(head, n, nth_last) :
global count
global data
# if list is empty
if (head == None):
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
def findNthFromLastUtil(head, n) :
global count
global data
# Initialize
nth_last = Node(0)
count = 0
# find nth node from the last
findNthFromLast(head, n, nth_last)
# if node exists, then print it
if (nth_last != None) :
print("Nth node from last is: " , data)
else:
print("Node does not exists")
# Driver Code
# linked list: 4.2.1.5.3
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)
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;
}
// function 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
JavaScript
<script>
// JavaScript implementation to recursively
// find the nth node from the last
// of the linked list
var count = 0,
data = 0;
// a node of a linked list
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
// function to get a new node
function getNode(data) {
// allocate space
var newNode = new Node();
// put in data
newNode.data = data;
newNode.next = null;
return newNode;
}
// function to recursively
// find the nth node from
// the last of the linked list
function findNthFromLast(head, n, 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
function findNthFromLastUtil(head, n) {
// Initialize
var 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)
document.write("Nth node from last is: " + data + "<br>");
else document.write("Node does not exists <br>");
}
// Driver Code
// linked list: 4.2.1.5.3
var 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);
var n = 2;
findNthFromLastUtil(head, n);
</script>
Output:
Nth node from last is: 5
Time Complexity: O(N), as we are using a loop to traverse N times, where N is the number of Nodes in the linked list.
Auxiliary Space: O(N) for call stack since using recursion
Similar Reads
XOR Linked List - Find Nth Node from the end
Given a XOR linked list and an integer N, the task is to print the Nth node from the end of the given XOR linked list. Examples: Input: 4 â> 6 â> 7 â> 3, N = 1 Output: 3 Explanation: 1st node from the end is 3.Input: 5 â> 8 â> 9, N = 4 Output: Wrong Input Explanation: The given Xor Li
15+ min read
Print the last k nodes of the linked list in reverse order | Recursive approach
Given a linked list containing N nodes and a positive integer 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, k = 4 Outpu
7 min read
Delete Nth node from the end of the given linked list
Given a linked list and an integer N, the task is to delete the Nth node from the end of the given linked list.Examples: Input: 2 -> 3 -> 1 -> 7 -> NULL, N = 1 Output: 2 3 1Explanation: The created linked list is: 2 3 1 7 The linked list after deletion is: 2 3 1Input: 1 -> 2 -> 3 -
10 min read
Remove Nth node from end of the Linked List
Given a linked list. The task is to remove the Nth node from the end of the linked list.Examples: Input : LinkedList = 1 ->2 ->3 ->4 ->5 , N = 2Output : 1 ->2 ->3 ->5Explanation: Linked list after deleting the 2nd node from last which is 4, is 1 ->2 ->3 ->5 Input : Link
15+ min read
Recursive function to delete k-th node from linked list
Given a singly linked list, delete a node at the kth position without using the loop. Examples: Input : list = 9->8->3->5->2->1 k = 4 Output : 9->8->3->2->1 Input : list = 0->0->1->6->2->3 k = 3 Output : 0->0->6->2->3 We recursively reduce the va
6 min read
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
Append the last M nodes to the beginning of the given linked list.
Given a linked list and an integer M, the task is to append the last M nodes of the linked list to the front.Examples: Input: List = 4 -> 5 -> 6 -> 1 -> 2 -> 3 -> NULL, M = 3 Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLInput: List = 8 -> 7 -> 0 -> 4 -> 1
13 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
Move the First Fibonacci Number to the End of a Linked List
Given a singly linked list, the task is to identify the first Fibonacci number in the list and move that node to the end of the linked list. Examples: Input: 10 -> 15 -> 8 -> 13 -> 21 -> 5 -> 2 -> NULLOutput: 10 -> 15 -> 13 -> 21 -> 5 -> 2 -> 8 -> NULLExplan
12 min read
Reverse a singly Linked List in groups of given size (Recursive Approach)
Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed.Example: Input: head: 1 -> 2 -> 3 -> 4 -> 5 ->
9 min read