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.
- Modify contents of Linked List - Recursive approach
- Recursive approach for alternating split of Linked List
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Recursive function to delete k-th node from linked list
- Recursive selection sort for singly linked list | Swapping node links
- Find Length of a Linked List (Iterative and Recursive)
- Find the balanced node in a Linked List
- Find the fractional (or n/k - th) node in linked list
- Find modular node in a linked list
- Find first node of loop in a linked list
- Find the largest node in Doubly linked list
- Find the second last node of a linked list in single traversal
- Find kth node from Middle towards Head of a Linked List
- Create new linked list from two given linked list with greater element at each node
- Swap Kth node from beginning with Kth node from end in a 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 email@example.com. 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.