<p>In this article, we will discuss how to reverse linked list in <strong><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">java</a></strong>. Iterative and Recursive there are two approaches, we can use to find the solution to this problem. As we know that, a linked list has several nodes, and each node in the list has the content and a pointer to the next node in the list. It does not store any pointer to the previous node. To store a single linked list, only the pointer to the first node in that list must be stored. The last node in a single linked list points to nothing.<br />
<img class="aligncenter wp-image-4245 size-full" src="https://dineshonjava.com/wp-content/uploads/2018/09/reverse-linkedlist.png" alt="reverse linked list" width="622" height="218" />In the previous articles, we have discussed several algorithms related to the liked list like the following:</p>
<ul>
<li><strong><a href="https://dineshonjava.com/nth-node-from-the-end-of-a-singly-linked-list/">Find the nth element in a linked list</a></strong></li>
<li><strong><a href="https://dineshonjava.com/find-the-middle-element-in-a-linked-list/">A middle element in a linked list</a></strong></li>
</ul>
<h2>Reverse linked list</h2>
<p><strong>Input</strong><br />
A linked list, the task is to reverse the linked list.</p>
<p><strong>Output</strong><br />
Reverse the linked list and return the head of the modified list.</p>
<p><strong>For Example:</strong></p>
<pre class="">Input : ->1->2->3->4->5->6->7->8->9 
 
Reversed : ->9->8->7->6->5->4->3->2->1 
</pre>
<p>Let&#8217;s discuss the following two approaches to reverse a linked list.</p>
<h3>Approach 1: Iterative</h3>
<p>In this approach, we will use iterative strategy to reverse linked list with the following steps:<br />
<strong>Step 1:</strong> We create 3 nodes such as <em>currentNode, previousNode</em> and <em>nextNode</em>.<br />
<strong>Step 2:</strong> Let&#8217;s initialize them as <em>currentNode = head, previousNode = null</em> and <em>nextNode = null</em>.<br />
<strong>Step 3:</strong> Now move these nodes and keep reversing these pointers one by one till <em>currentNode !</em>= null.<br />
<strong>Step 4:</strong> Finally, set <em>head = previousNode</em>.</p>
<p>Let&#8217;s see the code:</p>
<pre>private static Node reverseUsingIteration(Node head)	{ 
 if(head.getNext() == null) { 
 	return head; 
 } 
 Node currentNode = head; 
 Node nextNode = null; 
 Node previousNode = null; 
 		 
 while(currentNode != null) { 
 	nextNode = currentNode.getNext(); 
 	currentNode.setNext(previousNode); 
 	previousNode = currentNode; 
 currentNode = nextNode; 
 } 
 return previousNode; 
	} 
</pre>
<p>Let&#8217;s discuss another approach to reverse linked list using recursion.</p>
<h3>Approach 2: Recursive</h3>
<p>In this approach, we will use recursive strategy to reverse linked list with the following steps:</p>
<p><strong>Step 1:</strong> Let&#8217;s consider a node head and pass to the <em>reverseUsingRecursion(head)</em> method<br />
<strong>Step 2:</strong> If<em> head.next</em> is null return head.<br />
<strong>Step 3:</strong> Now <em>head.next</em> is not null the call <em>reverseUsingRecursion(head.next)</em><br />
<strong>Step 4:</strong> And set <em>head.next = head</em> and <em>head.next=null</em></p>
<p>Let&#8217;s see the code like the following:</p>
<pre>private static Node reverseUsingRecursion(Node head)	{ 
 if(head.getNext() == null) { 
 	return head; 
 } 
 Node newHead = reverseUsingRecursion(head.getNext()); 
 head.getNext().setNext(head); 
 head.setNext(null); 
 return newHead; 
	} 
</pre>
<p>Let&#8217;s see the complete code:</p>
<pre>/** 
 * 
 */ 
package com.dineshonjava.algo; 
 
import java.util.Optional; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class LinkedListTest { 
 
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args) { 
		 
		Node listNode = createLinkedList(9); 
		System.out.println("Print List Before Reverse"); 
		printList(listNode); 
		listNode = reverseUsingRecursion(listNode); 
		System.out.println("Print List After Reverse using Recusion"); 
		printList(listNode); 
		 
		System.out.println("Print List Before Reverse"); 
		printList(listNode); 
		listNode = reverseUsingIteration(listNode); 
		System.out.println("Print List After Reverse using Iteration"); 
		printList(listNode); 
	} 
	 
	private static Node createLinkedList(int n) { 
	 Node head = new Node("1"); 
	 Node current = head; 
	 
	 for (int i = 2; i <;= n; i++) { 
	 Node newNode = new Node(String.valueOf(i)); 
	 current.setNext(newNode); 
	 current = newNode; 
	 } 
	 
	 return head; 
	} 
	 
	private static Node reverseUsingIteration(Node head)	{ 
 if(head.getNext() == null) { 
 	return head; 
 } 
 Node currentNode = head; 
 Node nextNode = null; 
 Node previousNode = null; 
 		 
 while(currentNode != null) { 
 	nextNode = currentNode.getNext(); 
 	currentNode.setNext(previousNode); 
 	previousNode = currentNode; 
 currentNode = nextNode; 
 } 
 return previousNode; 
	} 
	 
	private static Node reverseUsingRecursion(Node head)	{ 
 if(head.getNext() == null) { 
 	return head; 
 } 
 Node newHead = reverseUsingRecursion(head.getNext()); 
 head.getNext().setNext(head); 
 head.setNext(null); 
 return newHead; 
	} 
	 
	public static void printList(Node head) { 
		while (head != null) { 
			System.out.print(head.getData()); 
			head = head.getNext(); 
	 } 
		System.out.println(); 
	} 
} 
 
</pre>
<p>Run above program, you will get the following output:</p>
<pre> 
Print List Before Reverse 
->1->2->3->4->5->6->7->8->9 
 
Print List After Reverse using Recusion 
->9->8->7->6->5->4->3->2->1 
 
Print List Before Reverse 
->9->8->7->6->5->4->3->2->1 
 
Print List After Reverse using Iteration 
->1->2->3->4->5->6->7->8->9 
</pre>
<p>Hope, you have understood this solution for the above to reverse a linked list. Please share other solutions if you have. :).</p>
<p>Happy learning with us!!!.</p>
<div align="center"><iframe style="width: 120px; height: 240px;" src="//ws-in.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&;OneJS=1&;Operation=GetAdHtml&;MarketPlace=IN&;source=ac&;ref=tf_til&;ad_type=product_link&;tracking_id=dineshonjav06-21&;marketplace=amazon&;region=IN&;placement=1787127567&;asins=1787127567&;linkId=a1e64f621b5e6128d8cb10e876eb1c48&;show_border=true&;link_opens_in_new_window=true&;price_color=333333&;title_color=0066c0&;bg_color=ffffff" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" align="center"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span><br />
</iframe></div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/find-the-middle-element-in-a-linked-list/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/delete-given-node-from-singly-linked-list/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4244'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…