1. Introduction

In Computer Science, a linked list is a linear data structure in which a pointer in each element determines the order. In this tutorial, we’ll show how to reverse a linked list.

2. Linked List Reversal

Each element of a linked list contains a data field to store the list data and a pointer field to point to the next element in the sequence. We can use a head pointer to point to the start element of a linked list:


After we reverse the linked list, the head will point to the last element of the original linked list, and the pointer of each element will point to the previous element of the original linked list:


3. Iterative Solution

Firstly, let’s solve a simpler version of the problem: reverse a linked list with two elements:


Suppose the current pointer points to the second element and the previous pointer points to the element before the current element, we can switch the link order between them with two operations:

current.next = previous
previous.next = null

For a linked list with more than two elements, we can traverse the linked list and use the same strategy to reverse the current element’s next pointer:

Rendered by QuickLaTeX.com

In this iterative algorithm, we first set the previous pointer as a null pointer and the current as the head. Then, in each iteration of the loop, we reverse the linked pointer of these two elements and shift the previous and current pointers to the next two elements. In the end, the previous pointer will point to the new head element of the reversed linked list.

Since each element only has one reference to the next element, we need another pointer, nextElement,  to store the next element before changing the current pointer.

The loop traverses the whole linked list once. Therefore, the running time of the iterative algorithm is O(n), where n is the total number of elements of the linked list.

4. Recursive Solution

We can also solve the problem with a recursive solution. Let’s first consider a simpler case where we have reversed the rest of the linked list after the head element:


We only need to reverse two elements: head and head.next. At the beginning, the next pointer of the head.next is null. We should change that to make it point to head. Then, we need to change the next pointer of head element to null to finish the reversal:

head.next.next = head
head.next = null

We can extend this solution to a recursive algorithm of reversing a linked list staring with a head element. Firstly, we can reverse the linked list starting with head.next element by recursively calling our reversal function.  Then, the linked list becomes our simpler case. Therefore, we can reverse the last two elements, head and head.next, with the above two operations.

We can construct a recursive algorithm based on this approach:

Rendered by QuickLaTeX.com

In this recursive algorithm, we first check base cases where the input head pointer is null or points to a single element. Then, we recursively call the reverseList function on the head.next element to reverse the rest of the linked list. Finally, we reverse head and head.next elements to finish the reversal.

The recursive algorithm also traverses the whole linked list once. Therefore, the running time is O(n), where n is the total number of elements of the linked list.

5. Conclusion

In this tutorial, we showed a sample linked list and its reversal. Also, we discussed two algorithms that can reverse a linked list in linear time.

Comments are closed on this article!