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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void findNthFromLast(Node* head, int n, int * count,
Node** nth_last)
{
if (!head)
return ;
findNthFromLast(head->next, n, count, nth_last);
*count = *count + 1;
if (*count == n)
*nth_last = head;
}
void findNthFromLastUtil(Node* head, int n)
{
Node* nth_last = NULL;
int count = 0;
findNthFromLast(head, n, &count, &nth_last);
if (nth_last != NULL)
cout << "Nth node from last is: "
<< nth_last->data;
else
cout << "Node does not exists" ;
}
int main()
{
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
import java.util.*;
class GFG
{
static int count = 0 , data = 0 ;
static class Node
{
int data;
Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void findNthFromLast(Node head, int n,
Node nth_last)
{
if (head == null )
return ;
findNthFromLast(head.next, n, nth_last);
count = count + 1 ;
if (count == n)
{
data = head.data;
}
}
static void findNthFromLastUtil(Node head, int n)
{
Node nth_last = new Node();
findNthFromLast(head, n, nth_last);
if (nth_last != null )
System.out.println( "Nth node from last is: " +
data);
else
System.out.println( "Node does not exists" );
}
public static void main(String args[])
{
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);
}
}
|
Python
count = 0
data = 0
class Node( object ):
def __init__( self , d):
self .data = d
self . next = None
def getNode(data):
newNode = Node( 0 )
newNode.data = data
newNode. next = None
return newNode
def findNthFromLast(head, n, nth_last) :
global count
global data
if (head = = None ):
return
findNthFromLast(head. next , n, nth_last)
count = count + 1
if (count = = n) :
data = head.data
def findNthFromLastUtil(head, n) :
global count
global data
nth_last = Node( 0 )
count = 0
findNthFromLast(head, n, nth_last)
if (nth_last ! = None ) :
print ( "Nth node from last is: " , data)
else :
print ( "Node does not exists" )
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)
|
C#
using System;
public class GFG
{
static int count = 0, data = 0;
class Node
{
public int data;
public Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void findNthFromLast(Node head, int n,
Node nth_last)
{
if (head == null )
return ;
findNthFromLast(head.next, n, nth_last);
count = count + 1;
if (count == n)
{
data = head.data;
}
}
static void findNthFromLastUtil(Node head, int n)
{
Node nth_last = new Node();
count = 0;
findNthFromLast(head, n, nth_last);
if (nth_last != null )
Console.WriteLine( "Nth node from last is: " +
data);
else
Console.WriteLine( "Node does not exists" );
}
public static void Main(String []args)
{
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);
}
}
|
Javascript
<script>
var count = 0,
data = 0;
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function findNthFromLast(head, n, nth_last) {
if (head == null ) return ;
findNthFromLast(head.next, n, nth_last);
count = count + 1;
if (count == n) {
data = head.data;
}
}
function findNthFromLastUtil(head, n) {
var nth_last = new Node();
count = 0;
findNthFromLast(head, n, nth_last);
if (nth_last != null )
document.write( "Nth node from last is: " + data + "<br>" );
else document.write( "Node does not exists <br>" );
}
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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 Jul, 2022
Like Article
Save Article