Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.
Note that the question is only about printing the reverse. To reverse the list itself see this
Difficulty Level: Rookie

Algorithm
printReverse(head)
1. call print reverse for hed->next
2. print head->data
[ad type=”banner”]
C Programming:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void printReverse(struct node* head)
{
if (head == NULL)
return;
printReverse(head->next);
printf("%d ", head->data);
}
void push(struct node** head_ref, char new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct node* head = NULL;
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printReverse(head);
return 0;
}
Output:
4 3 2 1
Time Complexity: O(n)
[ad type=”banner”]