Reverse a Linked List in C
发布时间:2026-09-17 | 浏览:1
Write a C program to reverse the linked list and also display the reversed linked list.
Linked List is a linear data structure in which nodes are connected with each other in a sequential manner. Each node contains the data and the address of the next node. The last node points to the NULL to terminate the list.
Example: 2 → 4 → 5 → NULL
Reverse Linked List is a linked list created to form a linked list by inverting the links within the list. The first node in the linked list is the last node in the linked list, and the last node is the first node.
Example: Linked List before reversed: 1 → 2 → 3 → 4 → 5 → NULL
Reversed Linked List: 5 → 4 → 3 → 2 → 1 → NULL
To solve this problem, we need to follow the given steps:
1. Given the head pointer of the linked list. 2. Reverse the order of the linked list by updating the pointer of the node. 3. Update the head of the list. 4. Print the linked list before and after reversing it. 5. Exit.
This problem can be solved by many approaches, but here we are discussing the following approaches:
Reverse a Linked List in C using Iterative Approach
Reverse a Linked List in C using Stack
Reverse a Linked List in C using Three Pointer
Reverse a Linked List in C using Recursion
Reverse a Linked List in C using Head Recursive Approach
Reverse a Linked List in C using Tail Recursive Approach
Another Linked List Example using Iterative Approach
The idea of this concept is to create a new list in reverse order. Create a node and insert it to the beginning of the list and update the head pointer.
Example: Input: Given the head of the linked list. 99 → 78 → 31 → 90 → 10 → NULL
Output: Print the linked list before and after reversing it.
Linked List before reversed: 99 → 78 → 31 → 90 → 10 → NULL
Linked List after reversed: 10 → 90 → 31 → 78 → 99 → NULL
Here is a source code of the C Program to reverse a linked list using iterative approach. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Reverse a Linked List using iterative
#include<stdio.h>
#include<malloc.h>
* A linked list node
struct node * next ;
//Globally initialized head pointer
struct node * head = NULL ;
//function prototyping
struct node * create_node ( int ) ;
void insert_begin ( int ) ;
void reverse_list ( ) ;
void print ( ) ;
/* Create some nodes and insert data into them */
insert_begin ( 10 ) ;
insert_begin ( 90 ) ;
insert_begin ( 31 ) ;
insert_begin ( 78 ) ;
insert_begin ( 99 ) ;
printf ( "Linked List before reversed: \n " ) ;
reverse_list ( ) ;
printf ( " \n Linked List after reversed: \n " ) ;
* Creates a new node using the malloc function
struct node * create_node ( int data )
struct node * new_node = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
if ( new_node == NULL )
printf ( "Memory can't be allocated for new node" ) ;
new_node -> data = data ;
new_node -> next = NULL ;
return new_node ;
* insert a new node at the beginning of the list
void insert_begin ( int data )
struct node * new_node = create_node ( data ) ;
if ( new_node != NULL )
new_node -> next = head ;
head = new_node ;
* reverse the linked list
void reverse_list ( )
if ( head == NULL )
struct node * temp = head ;
struct node * new_head = NULL ;
// create new nodes and insert them beginning
while ( temp != NULL )
struct node * new_node = create_node ( temp -> data ) ;
new_node -> next = new_head ;
new_head = new_node ;
temp = temp -> next ;
// update the head with the new head
head = new_head ;
* prints the linked list
struct node * temp = head ;
while ( temp != NULL )
printf ( "%d --> " , temp -> data ) ;
temp = temp -> next ;
printf ( "NULL \n " ) ;
1. Creating some nodes with data and appending them at the start of the linked list. 2. Print the list before reversing the original list. 3. Iterate through the list from start to end node. 4. On every iteration, create a new node and store the data of the current iterating node. 5. Insert this node at the beginning of the list. 6. In the end, Update the head pointer to the new head pointer. 7. Print the reversed list.
Time Complexity: O(n) Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.
Space Complexity: O(n) Another list of the same size is created so the space complexity becomes the factor of n, which is the size of the list.
In this method, the stack is implemented using the array. Stack is a linear data structure that stores the element in LIFO (Last In First Out) manner. Here the main concept is to store the all nodes of the list in Stack and then remove them one by one and insert it into the end of the list.
Here is a source code of the C Program to reverse the linked list using stack. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Reverse a Linked List using Stack
#include<stdio.h>
#include<malloc.h>
* A linked list node
struct node * next ;
//Globally initialized head pointer
struct node * head = NULL ;
//function prototyping
struct node * create_node ( int ) ;
void insert_begin ( int ) ;
void reverse_list ( ) ;
void print ( ) ;
/* Create some nodes and insert data into them */
insert_begin ( 10 ) ;
insert_begin ( 90 ) ;
insert_begin ( 31 ) ;
insert_begin ( 78 ) ;
insert_begin ( 99 ) ;
printf ( "Linked List before reversed: \n " ) ;
reverse_list ( ) ;
printf ( " \n Linked List after reversed: \n " ) ;
* Creates a new node using the malloc function
struct node * create_node ( int data )
struct node * new_node = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
if ( new_node == NULL )
printf ( "Memory can't be allocated for new node" ) ;
new_node -> data = data ;
new_node -> next = NULL ;
return new_node ;
* insert a new node at the beginning of the list
void insert_begin ( int data )
struct node * new_node = create_node ( data ) ;
if ( new_node != NULL )
new_node -> next = head ;
head = new_node ;
* reverse the linked list
void reverse_list ( )
if ( head == NULL )
//create a stack of size of 100
struct node * stack [ 100 ] ;
int top = - 1 ;
struct node * temp = head ;
// push list node into the stack
while ( temp != NULL )
stack [ top ] = temp ;
temp = temp -> next ;
// make a new head node
head = stack [ top ] ;
temp = stack [ top ] ;
// pop the nodes from the stack and insert them at the end of the last node
while ( -- top >= 0 )
temp -> next = stack [ top ] ;
temp = stack [ top ] ;
// terminate the list
temp -> next = NULL ;
* prints the linked list
struct node * temp = head ;
while ( temp != NULL )
printf ( "%d --> " , temp -> data ) ;
temp = temp -> next ;
printf ( "NULL \n " ) ;
1. Create some list nodes to perform the operations. 2. Print the list before reversing it. 3. Created a stack using the array. 4. Push the list nodes in Stack. 5. Update the head by the node which is at the top of the stack. 6. Remove one by one node from the stack and insert them at the end of the list. 7. Print the reversed list.
Time Complexity: O(n) Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.
Space Complexity: O(n) Since we have pushed all the nodes of the list in the stack so here n size of memory is used to store the list.
The main concept of the iterative approach is to use the three different pointers. These pointers will point to the particular nodes on every iteration and change the address of the nodes.
Iterative Algorithm Step 1: Take three pointers and initialize them as prev = NULL, next = NULL and curr = head. Step 2: Iterate through the end of the list and follow the next steps. Step 3: Store the address of the next node in the next pointer. Step 4: Update the curr next to the prev pointer. Step 5: Update the next pointer by curr pointer. Step 6: Move the curr pointer to the next node.
Here is a source code of the C Program to reverse the linked list using three pointer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Reverse a Linked List using three pointer
#include<stdio.h>
#include<malloc.h>
* A linked list node
struct node * next ;
//Globally initialized head pointer
struct node * head = NULL ;
//function prototyping
struct node * create_node ( int ) ;
void insert_begin ( int ) ;
void reverse_list ( ) ;
void print ( ) ;
/* Create some nodes and insert data into them */
insert_begin ( 10 ) ;
insert_begin ( 90 ) ;
insert_begin ( 31 ) ;
insert_begin ( 78 ) ;
insert_begin ( 99 ) ;
printf ( "Linked List before reversed: \n " ) ;
reverse_list ( ) ;
printf ( " \n Linked List after reversed: \n " ) ;
* Creates a new node using the malloc function
struct node * create_node ( int data )
struct node * new_node = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
if ( new_node == NULL )
printf ( "Memory can't be allocated for new node" ) ;
new_node -> data = data ;
new_node -> next = NULL ;
return new_node ;
* insert a new node at the beginning of the list
void insert_begin ( int data )
struct node * new_node = create_node ( data ) ;
if ( new_node != NULL )
new_node -> next = head ;
head = new_node ;
* reverse the linked list
void reverse_list ( )
struct node * prev = NULL , * curr = head , * next = NULL ;
while ( curr != NULL )
// store the next node
next = curr -> next ;
// reverse the pointer of the current node
curr -> next = prev ;
// move prev pointer to the current node
// move current to its next node
//update the head pointer by prev pointer
* prints the linked list
struct node * temp = head ;
while ( temp != NULL )
printf ( "%d → " , temp -> data ) ;
temp = temp -> next ;
printf ( "NULL \n " ) ;
1. Creating some nodes with data and appending them at the start of the linked list. 2. Print the list before reversing the original list. 3. Take three pointers to update the node’s address. 4. Iterate through the list from start to end node. 5. On every iteration, store the next node of the current node. 6. Point the current node to its previous node. 7. Move the previous and current pointers to their next node. 8. In the end, Update the head pointer to the start node of the reversed list. 9. Print the reversed list.
Time Complexity: O(n) Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.
Space Complexity: O(1) There are only three pointers used to reverse the list which is a constant space and it is not depending on the size of the list.
In this method, it 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.
* Recursive C program to reverse nodes of a linked list and display
#include <stdio.h>
#include <stdlib.h>
struct node * next ;
void print_reverse_recursive ( struct node * ) ;
void print ( struct node * ) ;
void create_new_node ( struct node *, int ) ;
//Driver Function
struct node * head = NULL ;
insert_new_node ( & head , 1 ) ;
insert_new_node ( & head , 2 ) ;
insert_new_node ( & head , 3 ) ;
insert_new_node ( & head , 4 ) ;
printf ( "LinkedList : " ) ;
print ( head ) ;
printf ( " \n LinkedList in reverse order : " ) ;
print_reverse_recursive ( head ) ;
printf ( " \n " ) ;
//Recursive Reverse
void print_reverse_recursive ( struct node * head )
if ( head == NULL )
//Recursive call first
print_reverse_recursive ( head -> next ) ;
printf ( "%d " , head -> data ) ;
//Print the linkedlist normal
void print ( struct node * head )
if ( head == NULL )
printf ( "%d " , head -> data ) ;
print ( head -> next ) ;
//New data added in the start
void insert_new_node ( struct node ** head_ref , int 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 ;
Time Complexity: O(n) The function is calling itself n times, which is the size of the linked list.
Space Complexity: O(n) The maximum size of the function call is n, which stores each node of the list on every call onto the stack.
Recursive functions use a stack to store the function calls. To solve this problem we recursively iterate the function calls to each node at the end of the list and then start the address to the previous node.
Recursive Algorithm Step 1: Divide the linked list into two parts, the first part contains the first node, and the second part the remaining list. Step 2: Call again the reverse_list_recursion() function by passing the head of the remaining list. Step 3: Merge the remaining list to the first node and change the head pointer of the reversed list. Step 4: Repeat the above steps until the entire list doesn’t get reversed.
Here is a source code of the C Program to reverse the linked list using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Reverse a Linked List using head recursive approach
#include<stdio.h>
#include<malloc.h>
struct node * next ;
//Global head pointer
struct node * head = NULL ;
//function prototyping
struct node * create_node ( int ) ;
void insert_begin ( int ) ;
struct node * reverse_recursion ( struct node * ) ;
void print ( ) ;
/* Create some nodes and insert data into them */
insert_begin ( 10 ) ;
insert_begin ( 90 ) ;
insert_begin ( 31 ) ;
insert_begin ( 78 ) ;
insert_begin ( 99 ) ;
printf ( "Linked List before reversed: \n " ) ;
head = reverse_recursion ( head ) ;
printf ( " \n Linked List after reversed: \n " ) ;
struct node * create_node ( int data )
struct node * new_node = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
if ( new_node == NULL )
printf ( "Memory can't be allocated for new node" ) ;
new_node -> data = data ;
new_node -> next = NULL ;
return new_node ;
void insert_begin ( int data )
struct node * new_node = create_node ( data ) ;
if ( new_node != NULL )
new_node -> next = head ;
head = new_node ;
struct node * reverse_recursion ( struct node * head )
// if only one node or the last node of the list
if ( head == NULL || head -> next == NULL )
struct node * new_head = reverse_recursion ( head -> next ) ;
//update the next pointer of the current node
head -> next -> next = head ;
//make the current node the last node
head -> next = NULL ;
//return the new head node of the reversed list
return new_head ;
struct node * temp = head ;
while ( temp != NULL )
printf ( "%d → " , temp -> data ) ;
temp = temp -> next ;
printf ( "NULL \n ”);
1. Creating some nodes with data and inserting them at the start of the linked list. 2. Print the list before reversing the original list. 3. Call the function passing with the head node of the list. 4. The function checks whether the node is NULL or the last node of the list. 5. Pass the head of the remaining list on the next function call, which is the next node of the current node and stores the address of the returned new head node. 6. Update the next node by pointing it back to the current node. 7. Make the current node the last node. 8. Return new head of the list. 9. Print the reversed list.
Time Complexity: O(n) The function is calling itself n times, which is the size of the linked list.
Space Complexity: O(n) The maximum size of the function call is n, which stores each node of the list on every call onto the stack.
In tail recursive solutions, we have to do operations before calling the recursive function. To reverse the list, call the function each time passing with the next node of the current node and the last node. Point the current node by the last node.
Here is a source code of the C Program to reverse the linked list using tail recursive approach. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Reverse a Linked List using tail recursion
#include<stdio.h>
#include<malloc.h>
* A linked list node
struct node * next ;
//Globally initialized head pointer
struct node * head = NULL ;
struct node * last = NULL ;
//function prototyping
struct node * create_node ( int ) ;
void insert_begin ( int ) ;
void reverse_list ( ) ;
void print ( ) ;
/* Create some nodes and insert data into them */
insert_begin ( 10 ) ;
insert_begin ( 90 ) ;
insert_begin ( 31 ) ;
insert_begin ( 78 ) ;
insert_begin ( 99 ) ;
printf ( "Linked List before reversed: \n " ) ;
reverse_list ( head , last ) ;
printf ( " \n Linked List after reversed: \n " ) ;
* Creates a new node using the malloc function
struct node * create_node ( int data )
struct node * new_node = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
if ( new_node == NULL )
printf ( "Memory can't be allocated for new node" ) ;
new_node -> data = data ;
new_node -> next = NULL ;
return new_node ;
* insert a new node at the beginning of the list
void insert_begin ( int data )
struct node * new_node = create_node ( data ) ;
if ( new_node != NULL )
new_node -> next = head ;
head = new_node ;
* reverse the linked list
void reverse_list ( struct node * curr , struct node * last )
if ( curr == NULL )
struct node * next_node = curr -> next ;
curr -> next = last ;
//updating the head node
reverse_list ( next_node , last ) ;
* prints the linked list
struct node * temp = head ;
while ( temp != NULL )
printf ( "%d --> " , temp -> data ) ;
temp = temp -> next ;
printf ( "NULL \n " ) ;
1. Create some nodes and insert them at the beginning of the list. 2. Print the list before recursion. 3. Store the next node of the current node. 4. Update the next pointer of the current node by the last pointer. 5. Change the last pointer to the current pointer. 6. Update the head of the list by the current pointer. 7. Call again the same function by passing the next node of the current node and the last node. 8. Print the reversed list.
Time Complexity: O(n) Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.
Space Complexity: O(n) We are recursively iterating the list from start to end, which is the size of the list
Here is a source code of the C Program to reverse a linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Reverse a Linked List
#include <stdio.h>
#include <stdlib.h>
struct node * next ;
void create ( struct node ** ) ;
void reverse ( struct node ** ) ;
void release ( struct node ** ) ;
void display ( struct node * ) ;
struct node * p = NULL ;
printf ( "Enter data into the list \n " ) ;
create ( & p ) ;
printf ( "Displaying the nodes in the list: \n " ) ;
display ( p ) ;
printf ( "Reversing the list... \n " ) ;
reverse ( & p ) ;
printf ( "Displaying the reversed list: \n " ) ;
display ( p ) ;
release ( & p ) ;
void reverse ( struct node ** head )
struct node * p , * q , * r ;
p = q = r = * head ;
p = p -> next -> next ;
q = q -> next ;
r -> next = NULL ;
q -> next = r ;
while ( p != NULL )
p = p -> next ;
q -> next = r ;
void create ( struct node ** head )
struct node * temp , * rear ;
printf ( "Enter number: " ) ;
scanf ( "%d" , & c ) ;
temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
temp -> num = c ;
temp -> next = NULL ;
if ( * head == NULL )
* head = temp ;
rear -> next = temp ;
printf ( "Do you wish to continue [1/0]: " ) ;
scanf ( "%d" , & ch ) ;
} while ( ch != 0 ) ;
printf ( " \n " ) ;
void display ( struct node * p )
while ( p != NULL )
printf ( "%d \t " , p -> num ) ;
p = p -> next ;
printf ( " \n " ) ;
void release ( struct node ** head )
struct node * temp = * head ;
* head = ( * head ) -> next ;
while ( ( * head ) != NULL )
free ( temp ) ;
temp = * head ;
( * head ) = ( * head ) -> next ;
To practice programs on every topic in C, please visit “Programming Examples in C” , “Data Structures in C” and “Algorithms in C” .
Continue Learning
C Program to Solve Josephus Problem using Linked List
C Program to Implement Circular Doubly Linked List
C Program to Check if Singly Linked List is Palindrome
C Program to Implement Adjacency List
Doubly Linked List Program in C
C Program to Implement Binary Tree using Linked List
C Program to Remove Duplicates from a Linked List
C Program Find the Length of Linked List using Recursion
Apply for Computer Science Internship
Practice Computer Science MCQs
Check Data Structure Books
Check Computer Science Books
Practice Design & Analysis of Algorithms MCQ
I’m Manish , Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.
LinkedIn · YouTube MasterClass · Telegram Classes · Career Guidance & Conversations