Find the nth node from the end in the given linked list using a recursive approach.
Input : list: 4->2->1->5->3 n = 2 Output : 5
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().
Nth node from last is: 5
Time Complexity: O(n), where ‘n’ is the number of nodes in the linked list.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Delete Nth node from the end of the given linked list
- Remove Nth node from end of the Linked List
- Recursive approach for alternating split of Linked List
- Modify contents of Linked List - Recursive approach
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Swap Kth node from beginning with Kth node from end in a Linked List
- Swap Kth node from beginning with Kth node from end in a Doubly Linked List
- Recursive selection sort for singly linked list | Swapping node links
- Recursive function to delete k-th node from linked list
- Program for n'th node from the end of a Linked List
- Create new linked list from two given linked list with greater element at each node
- Find Length of a Linked List (Iterative and Recursive)
- Write a function to get Nth node in a Linked List
- Level order traversal with direction change after every two levels | Recursive Approach
- Count all sub-sequences having product <= K - Recursive approach
- Remove all occurrences of a character in a string | Recursive approach
- Search an element in a Linked List (Iterative and Recursive)
- Recursive insertion and traversal linked list
- Check if linked list is sorted (Iterative and Recursive)
- A Programmer's approach of looking at Array vs. Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.