C Program to Reverse a Singly Linked List using Iteration

Write a C program to reverse a singly linked list. Given a linked list, write a method to reverse a singly linked list. In this tutorial, we are going to write a code to reverse a linked list using an iterative approach.

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 you understand the concept and practice it very well.

C Program to Reverse a linked list using recursion

Also, we are using an iterative approach to solve this problem. If you are not familiar with iterative and recursive approach then check my previous tutorial on Recursion vs iteration.

Algorithm to Reverse a Singly 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).

Subscribe Our Tutorials

Get Latest Updates on Facebook

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

Program to Reverse a Linked List

Program to Reverse a Linked List

 

C Program to Reverse a Singly Linked List

We have discussed the algorithm to reverse a 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.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , , . Bookmark the permalink.