How to Write C Program to Reverse a Linked List Elements?

Linked list is the crucial data structure which contents list of the nodes. Each node has data variable to store data and node pointer to point next node in the linked list. In the previous post, we have seen the implementation of the Linked List in C. In this post, we see how to reverse the elements of linked list using three pointers.

The original Linked list will be passed to the function to reverse. It will exchange the links between every two consecutive nodes. This reverse a Linked List function will maintain two lists: one linked list contents reverse elements and second linked list contents remaining elements that need to be reversed.

Reverse a Linked list is one of the important questions to be asked in many placement interviews. I was asked to write code for this program in IBM ISDL placement.

Logically reverse a linked list function seems kinda difficult, but if you see the code for it, it is so simple and easy to understand.

Difficulty Level: Medium

Time Complexity: O(n)

We are traversing each element at one time, so its complexity is O(n).

I am complete Python Nut, love Linux and vim as an editor. I hold Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack Portal.