There are two ways to reverse a linked list in Java. Iterative and recursive. I will explain the iterative method of implementation. We will go through the example and present in the details each step of the process. If you want to know how to create the link list follow this page.
The main principle of switching items from back to front.
The primary idea is switching connections and values in those items with the help of temporary variables. I call it a “temporary variables technique” because they help us save some value for future usage.
To reverse the linked list, we need to go through that list, change places, and set new connections between them.

Code example of reverse a linked list (Java).
Let’s divide code into separate parts and explain each of them:
- As I said earlier, we will need some temporary variables to save the data on each iteration:
- ongoing – it is an item that is active in the current loop iteration. On the attached image, it is a green color
- prior – it is an item that goes BEFORE the current (ongoing) item. We need that item because we will connect all items back to the front. So our last item will move from the right side to the left side. On the attached image, it is blue color
- next – it is an item that goes AFTER the current (ongoing) item. We need that item to rotate it with the current one. In the attached image, it is red.
ListNode ongoing = head;
ListNode prior = null;
ListNode next = null;
- The “while” loop. In this loop, with the temporary variables’ help, we are switching the values between the ongoing and next items. We are also changing connections between that item and moving the head of the linked list from the left to the right side. Another important thing is that we are moving the null tail from the right side to the left. As a result, the linked list reverses from back to front.
- We are showing the result. We are just iterating the result of the second step. One important thing is that our head has now been moved to the right side, given that we start iteration from the prior variable.
Full code example:
private void reverseLinkedList() {
//You need to create linked list first
creatingLinkedList();
//Setting the head of the list
ListNode current = head;
//Initialize temporary variables
//Finding the first item from the list
ListNode ongoing = head;
ListNode prior = null;
ListNode next = null;
//Going through the list until last item don't have connection
while (ongoing != null) {
//Pointing the next item
next = ongoing.next;
//Reversing the selected item to previous and set it next value to previous item (might be null if in begin of the list)
ongoing.next = prior;
//Before move to next item since all manipulation is done make selected item as previous
prior = ongoing;
//Moving our selection to the next item
ongoing = next;
}
//Showing the results in logs. Previous is a new head of the linked list
while (prior != null) {
Log.d("linkedList", "-> " + prior.data);
prior = prior.next;
}
}
In this article, you figured out how to reverse the linked list java in an iterative way.
Full code and more examples in my facebook group.
Useful links:
Leave Any Questions Related To This Article Below!