Print the last k nodes of the linked list in reverse order Recursive Approaches in C language

In C programming, printing the last k nodes of a linked list in reverse order using recursion is an elegant approach that leverages the function call stack. This technique traverses the entire list recursively and then prints the nodes during the return phase of recursion.

Syntax

void printLastKNodesReverse(struct node* head, int* count, int k);

Algorithm

The recursive algorithm works in two phases −

  • Forward traversal: Recursively traverse to the end of the list
  • Backward counting: During return, increment counter and print nodes if count ? k
Linked List: 11 ? 243 ? 321 ? 421 ? 522 11 243 321 421 522 For k=2, last 2 nodes (highlighted) printed in reverse: Output: 522 421 Recursion unwinds from right to left, printing last k nodes

Example

Here's a complete implementation to print the last k nodes in reverse order −

#include <stdio.h>
#include <stdlib.h>

// Structure of a node
struct node {
    int data;
    struct node* next;
};

// Function to create a new node
struct node* createNode(int data) {
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}

// Recursive function to print last k nodes in reverse order
void printLastKNodesReverse(struct node* head, int* count, int k) {
    if (head == NULL)
        return;
    
    // Recursive call to traverse to the end
    printLastKNodesReverse(head->next, count, k);
    
    // Increment count during return phase
    (*count)++;
    
    // Print node if it's among the last k nodes
    if (*count <= k)
        printf("%d ", head->data);
}

int main() {
    // Creating linked list: 11 -> 243 -> 321 -> 421 -> 522
    struct node* head = createNode(11);
    head->next = createNode(243);
    head->next->next = createNode(321);
    head->next->next->next = createNode(421);
    head->next->next->next->next = createNode(522);
    
    int k = 2;
    int count = 0;
    
    printf("Linked List: ");
    struct node* temp = head;
    while (temp != NULL) {
        printf("%d", temp->data);
        if (temp->next != NULL) printf(" -> ");
        temp = temp->next;
    }
    printf("<br>");
    
    printf("Last %d nodes in reverse order: ", k);
    printLastKNodesReverse(head, &count, k);
    printf("<br>");
    
    return 0;
}
Linked List: 11 -> 243 -> 321 -> 421 -> 522
Last 2 nodes in reverse order: 522 421

How It Works

The recursive function works as follows −

  1. Base case: If head is NULL, return immediately
  2. Recursive call: Call function with head->next
  3. Count increment: When recursion unwinds, increment count
  4. Conditional print: Print node data if count ? k

Key Points

  • Time Complexity: O(n) where n is the number of nodes
  • Space Complexity: O(n) due to recursive call stack
  • The count parameter must be passed by reference using pointer
  • Nodes are printed in reverse order of their position from the end

Conclusion

The recursive approach elegantly solves the problem by using the call stack to reverse the traversal order. It's intuitive but uses additional stack space proportional to the list size.

Updated on: 2026-03-15T11:58:04+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements