C Program to Reverse a Linked List

Write a C program to reverse a linked list. Given a singly linked list, we have to write a code to reverse a linked list. In this tutorial, we are going to use iterative approach to solve this problem.

There are two ways to reverse a linked list. We can reverse a linked list using an iterative approach or we can reverse a linked list using recursion. Both approaches are very important in terms of interviews. So make sure you understand both the concepts and practice it very well.

C Program to Reverse a linked list using recursion

If you are not familiar with iterative and recursive approach then check my previous tutorial on Recursion vs iteration.

Algorithm to Reverse a Linked List

1. Traverse a linked list.

C program to count number of nodes in a linked list

2. Declare a three-pointers prev, next and current of a node type. The current pointer points to head. Using current pointer traverse a linked list until current node is not null. Change the links (or pointer to reverse a linked list).

The time complexity to reverse a linked list is O(n).

Program to Reverse a Linked List
C Program to Reverse a Linked List

 

C Program to Reverse a Linked List

We have discussed the algorithm to reverse a singly linked list. Let’s write a c code to implement an algorithm to reverse a singly linked list.

Output :

Books on Data Structures

I have tried my best to explain how to reverse a singly linked list in a very simple and easy way. If you still have any doubt and you want to share another approach with our readers then you can let us know through your comments.