
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print the last k nodes of the linked list in reverse order Recursive Approaches in C language
The task is to print the k nodes starting from the end of the linked list using recursive approach.
Recursive approach is the one in which the function call itself again and again till the call is being made and hence stores the result.
Let’s say, list contains the nodes 29, 34, 43, 56 and 88 and the value of k is 2 than the output will be the last k nodes such as 56 and 88.
Example
Linked List: 29->34->43->56->88 Input: 2 Output: 88 56
As specified the recursive approach should be used which will traverse the list from the end keeping track of the number of times the list is to be traversed and the recursive function will be called till the kth value by a pointer variable.
The below code shows the c implementation of the algorithm given.
Algorithm
START Step 1 -> create node variable of type structure Declare int data Declare pointer of type node using *next Step 2 -> Declare function as node* get(int data) Create newnode using malloc function Set newnode->data = data Set newnode->next = NULL return newnode step 3 -> Declare function void lastval(node* head, int& count, int k) IF !head Return Set lastval(head->next, count, k) Set count++ IF (count <= k) Print head->data Step 4 -> In Main() Generate head using node* head = get(11) Set k and count to 0 Call lastval(head,k,count) STOP
Example
#include<stdio.h> #include<stdlib.h> // Structure of a node struct node { int data; node* next; }; // Function to get a new node node* get(int data) { struct node* newnode = (struct node*)malloc(sizeof(struct node)); newnode->data = data; newnode->next = NULL; return newnode; } //print the last k values of a node void lastval(node* head, int& count, int k) { if (!head) return; lastval(head->next, count, k); count++; if (count <= k) printf("%d ", head->data); } int main() { node* head = get(11); //inserting elements into the list head->next = get(243); head->next->next = get(321); head->next->next->next = get(421); head->next->next->next->next = get(522); int k = 2, count = 0; printf(" last %d nodes of a list are :",k); // print last k nodes lastval(head, count, k); return 0; }
Output
If we run above program then it will generate following output.
last 2 nodes of a list are :522 421
- Related Articles
- Print the last k nodes of the linked list in reverse order Iterative approach in C language
- Print the alternate nodes of linked list (Iterative Method) in C language
- Print nodes of linked list at given indexes in C language
- Reverse Alternate K Nodes in a Singly Linked List in C++
- Print reverse of a Linked List without actually reversing in C language
- Print Immutable Linked List in Reverse in C++
- Find the product of last N nodes of the given Linked List in C++
- Find the product of first k nodes of the given Linked List in C++
- Using iterative function print the given number in reverse order in C language
- Print alternate nodes of a linked list using recursion in C++
- Program to reverse inner nodes of a linked list in python
- Reverse Nodes in k-Group in C++
- Python program to create a Circular Linked List of n nodes and display it in reverse order
- Python program to create a doubly linked list of n nodes and display it in reverse order
- An interesting method to print reverse of a linked list in C++

Advertisements