C Program to Reverse a Linked List using Recursion

This C Program uses recursive function & reverses the nodes in a linked list and displays the list. A linked list is an ordered set of data elements, each containing a link to its successor. This program makes each data element to link to its predecessor.

Here is the source code of the C program to reverse the nodes and display the linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * Recursive C program to reverse nodes of a linked list and display 
  3.  * them
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int data;
  11.     struct node *next;
  12. };
  13.  
  14. void print_reverse_recursive (struct node *);
  15. void print (struct node *);
  16. void create_new_node (struct node *, int );
  17.  
  18. //Driver Function
  19. int main ()
  20. {
  21.     struct node *head = NULL;
  22.     insert_new_node (&head, 1);
  23.     insert_new_node (&head, 2);
  24.     insert_new_node (&head, 3);
  25.     insert_new_node (&head, 4);
  26.     printf ("LinkedList : ");
  27.     print (head);
  28.     printf ("\nLinkedList in reverse order : ");
  29.     print_reverse_recursive (head);
  30.     printf ("\n");
  31.     return 0;
  32. }
  33.  
  34. //Recursive Reverse
  35. void print_reverse_recursive (struct node *head)
  36. {
  37.     if (head == NULL)
  38.     {
  39.         return;
  40.     }
  41.  
  42.     //Recursive call first
  43.     print_reverse_recursive (head -> next);
  44.     //Print later
  45.     printf ("%d ", head -> data);
  46. }
  47.  
  48. //Print the linkedlist normal
  49. void print (struct node *head)
  50. {
  51.     if (head == NULL)
  52.     {
  53.         return;
  54.     }
  55.     printf ("%d ", head -> data);
  56.     print (head -> next);
  57. }
  58.  
  59. //New data added in the start
  60. void insert_new_node (struct node ** head_ref, int new_data)
  61. {
  62.     struct node * new_node = (struct node *) malloc (sizeof (struct node));
  63.     new_node -> data = new_data;
  64.     new_node -> next = (*head_ref);
  65.     (*head_ref) = new_node;
  66. }

$ cc pgm.c
$ a.out
LinkedList : 4 3 2 1 
LinkedList in reverse order : 1 2 3 4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Linked List, go to Linked List. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.