DATA STRUCTURES PROGRAM • SINGLY LINKED LISTS
Reverse a Singly Linked List Recursively
Learn how to reverse a singly linked list recursively using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
30 20 10
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *next; };
struct Node *create_node(int value)
{
struct Node *node = malloc(sizeof *node);
if (node == NULL) { perror("malloc"); exit(EXIT_FAILURE); }
node->data = value;
node->next = NULL;
return node;
}
void append(struct Node **head, int value)
{
struct Node *node = create_node(value);
if (*head == NULL) { *head = node; return; }
struct Node *current = *head;
while (current->next != NULL) current = current->next;
current->next = node;
}
void print_list(const struct Node *head)
{
while (head != NULL) { printf("%d ", head->data); head = head->next; }
putchar('\n');
}
void free_list(struct Node *head)
{
while (head != NULL) { struct Node *next = head->next; free(head); head = next; }
}
struct Node *reverse_recursive(struct Node *head)
{
if (head == NULL || head->next == NULL) return head;
struct Node *new_head = reverse_recursive(head->next);
head->next->next = head;
head->next = NULL;
return new_head;
}
int main(void)
{
struct Node *head = NULL;
append(&head, 10); append(&head, 20); append(&head, 30);
head = reverse_recursive(head);
print_list(head);
free_list(head);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
30 20 10
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Reverse the suffix recursively and attach the current node behind its former successor.
- Display the computed result.
Reverse the suffix recursively and attach the current node behind its former successor.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(n) call stack
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
