Java Program to reverse a Linked List ?
- A Linked list contains two things data and the address of the node each node is linked to the next node.
- There can be two solution to reverse a linked list in java.
Iterative
- Here we have three nodes i.e previousNode, currentNode and nextNode
- When currentNode is starting node, then previousNode will be null
- Assign currentNode.next to previousNode to reverse the link.
- In each iteration move currentNode and previousNode by 1 node.
public static Node reverseLinkedList(Node currentNode)
{
// For first node, previousNode will be null
Node previousNode=null;
Node nextNode;
while(currentNode!=null)
{
nextNode=currentNode.next;
// reversing the link
currentNode.next=previousNode;
// moving currentNode and previousNode by 1 node
previousNode=currentNode;
currentNode=nextNode;
}
return previousNode;
}
Sample Code in Java:
public class LinkedList{
private Node head;
private static class Node {
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
public void addToTheLast(Node node) {
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
public void printList(Node head) {
Node temp = head;
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
// Reverse linkedlist using this function
public static Node reverseLinkedList(Node currentNode)
{
// For first node, previousNode will be null
Node previousNode=null;
Node nextNode;
while(currentNode!=null)
{
nextNode=currentNode.next;
// reversing the link
currentNode.next=previousNode;
// moving currentNode and previousNode by 1 node
previousNode=currentNode;
currentNode=nextNode;
}
return previousNode;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Creating a linked list
Node head=new Node(5);
list.addToTheLast(head);
list.addToTheLast(new Node(6));
list.addToTheLast(new Node(7));
list.addToTheLast(new Node(1));
list.addToTheLast(new Node(2));
list.printList(head);
//Reversing LinkedList
Node reverseHead=reverseLinkedList(head);
System.out.println("After reversing");
list.printList(reverseHead);
}
}
Output :
Categorized in:
Tagged in:
Accenture interview questions and answers, Applied Materials interview questions and answers, Atos interview questions and answers, Capgemini interview questions and answers, CASTING NETWORKS INDIA PVT LIMITED interview questions and answers, CGI Group Inc interview questions and answers, Chetu interview questions and answers, Ciena Corporation interview questions and answers, circular linked list java, Collabera Technologies interview questions and answers, compro technologies interview questions and answers, create linked list java, Dell International Services India Pvt Ltd interview questions and answers, doubly linked list java, FIS Global Business Solutions India Pvt Ltd interview questions and answers, Flipkart interview questions and answers, how to implement linked list in java, IBM interview questions and answers, Indecomm Global Services interview questions and answers, Infosys Technologies interview questions and answers, javascript linked list, L&T Infotech interview questions and answers, linked list class in java, linked list data structure in java, linked list implementation in java, linked list implementation in java source code, linked list java code, linked list java tutorial, linked list program in java, Mphasis interview questions and answers, NetApp interview questions and answers, Oracle Corporation interview questions and answers, PeopleStrong interview questions and answers, Persistent Systems interview questions and answers, RBS India De interview questions and answers, Reliance Industries Ltd interview questions and answers, reverse linked list java, SAP Labs India Pvt Ltd interview questions and answers, singly linked list java, sort linked list java, Tech Mahindra interview questions and answers, UnitedHealth Group interview questions and answers, Virtusa Consulting Services Pvt Ltd interview questions and answers, Wells Fargo interview questions and answers, Wipro Infotech interview questions and answers