C Programming

How to Reverse a linked list in C

The linked list has been defined as a structure-type set of nodes that contains some data. This guide will show how a linked list can be reversed in C language using Ubuntu 20.04 system. So, let’s get started by adding some examples. The user needs to open the terminal console on Ubuntu 20.04 desktop to work on the C language. To open it, we have used the shortcut key command “Ctrl+Alt+T”. Another way to launch it on your Ubuntu desktop is via the activity option on the desktop.

Example 01

As the terminal app is launched, create a new C file. We have to utilize a most used “touch” query in the terminal to do so, as shown below.

$ touch reverse.c

Now the C file has been created, open it with any Editor already installed and configured on your Linux system. We have been using the “Nano” editor to open and edit the C file.

$ nano reverse.c

Now the file has been shown in GNU Nano editor. We have included some libraries first to make the code executable in the terminal and by the compiler. A linked list node has been constructed as of struct type consist of node information variable and creation of next node. A method “reverse” has been defined for the reversing of a linked list. It constructs the pointer for address saving for the previous node, current or head node, and next node. While the head node is NULL, it is used to make it the next or current node. This has been done by using pointers so far.

The function “push” has been created to push data into nodes. It created a new node and assigned it some memory by the “malloc” method. The data has been assigned to a new node by passing arguments in parameters using head node references. The method show () has been used here to display a push function’s user’s information in the nodes.

We have defined the main method for the execution of a code. The starting node has been defined as NULL. After that, we have pushed the values within the head node using the pointer. After that, the show() method has been called here to display the messages. After that, the reverse() method has been called here to reverse the value of a linked list by binding the header pointer indicated by the node. Again, the show() method has been called to show the reverse linked list.

Let’s compile our code with GCC compiler in the terminal as per the stated command. No compilation errors have been found so far. The file was executed after that. It shows the original linked list first, then the Reverse Linked list as per the snap output below.

$ gcc reverse.c
$ ./a.out

Example 02

Our first example indicated the creation of a linked list and adding data into it manually. We will create a link and add data into it at run time to show and reverse the linked list. Open up the same file once again using the “GNU Nano” editor.

$ nano reverse.c

The same header libraries have been included while the struct type node has been created. Three main methods have been defined. The execution has been initialized from the main() method. It constructs a node pointer as Null. The create method has been called while binding the pointer within its parameters. The show() method has been displayed in the original linked list. On the other hand, the reverse() method has been called to reverse the linked list. The reversed linked list has been displayed after that.

After the calling of creating () method, the below code will be executed. Two struct-type pointers have been made along with 2 integers; the user will add a value for a respective node. This value will be assigned to its respective node by a pointer “temp”.  The user has been asked to either continue adding data or quit by adding 1 or 0.

The reverse method has been here to add reverse the data of a linked list. The While loop has been used here to reverse the linked list using its pointers.

The show() method has been defined here to print the data added to the linked list.

The compilation and execution have been done by using the same two commands. The user enters the node number and its respective value. After quitting, the original and reversed linked list has been shown on the screen.

$ gcc reverse.c
$ ./a.out

Conclusion

This article is best at the demonstration of reversing the linked list in the C language. The reversing of the linked list also contains creating a linked list and pushing data into nodes. In the end, the display methods have been used to display the contents in original and reverse order.

About the author

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content