Reverse a linked list by changing links between nodes
March 17, 2023
Reverse a linked list by changing links between nodes
Here we are learning Reverse a Linked List by changing links between nodes. Now the question arises is that how we can reverse a linked list nodes ?
To reverse a node in a singly Linked list we have to take a pointer to the head of singly linked list, after that we have to reverse the linked list and we also have to return the pointer to the head of reversed singly linked list
How to Reverse a Linked List by Changing Links Between Nodes
One option to reverse a linked list is to divide your linked list in two parts first part contains your head node and other part contains the rest of the list then remove the 0th node and create a new node from the 1st node. We will conduct learning of this topic in this page with the help of a java program and we also learn about it’s algorithm.
Algorithm
step 1– intialize a class
step 2– input int data;
node next;
step 3– input the linked list constructor.
(this.head = null)
(this.tail = null)
(this.size = 0)
step 4– Add a function to find out the size of a linked list (this.size)
Step 5– check whether the linked list is empty or not
Step 6– return the size of a linked list
step 7– traverse throughout the list
Step 8– print (temp.data+” “)
Step 9– Furthermore add a node in the beginning of linked list
step 10– check if linked list is empty, remember temp is the head and tail node both.
(this.size == 0)
Step 11– throw a new exception (“Index out of bound”)
Step 12– run a while loop to reverse the linked list.
while (n.next != null)
Step 13– swap the head and tail nodes of a linked list.
node temp = this.head
this.head = this.tail
this.tail = temp
Step 14– your linked list is reversed from it’s link node
import java.util.*;
class Main
{
public static void main (String[]args) throws Exception
{
LinkedList ll = new LinkedList ();
ll.addFirst (8);
ll.addFirst (12);
ll.addFirst (16);
ll.addFirst (24);
System.out.println("Linked List before reversal");
ll.display ();
ll.reversePI ();
System.out.println("Linked List after reversal");
ll.display ();
}
}
class LinkedList
{
private class Node
{
int data;
Node next;
// Node constructor
// There are two fields in the node- data and address of next node
public Node (int data, Node next)
{
this.data = data;
this.next = next;
}
}
private Node head;
private Node tail;
private int size;
// Linked list constructor
public LinkedList ()
{
this.head = null;
this.tail = null;
this.size = 0;
}
// Function to find the size of linked list
public int size ()
{
return this.size;
}
// Function to check whether linked list is empty or not
public boolean isEmpty ()
{
return this.size () == 0;
}
// Function to traverse and print the linked list
public void display ()
{
Node temp = head;
while (temp != null)
{
System.out.print (temp.data + " ");
// Set temp to point to the next node
temp = temp.next;
}
System.out.println(" ");
}
// Function to add a node in beginning of linked list
public void addFirst (int item)
{
// Create a temp node which points to head
Node temp = new Node (item, head);
// If linked list is empty, temp is the head and tail node both
if (this.size == 0)
{
this.head = this.tail = temp;
}
// else set the head such that it now points to temp node
else
{
this.head = temp;
}
this.size++;
}
// Auxiliary function to get node at any index
private Node getNodeAt (int index) throws Exception
{
if (index < 0 || index >= this.size)
{
throw new Exception ("Index out of bound");
}
int i = 0;
Node temp = head;
while (i < index)
{
temp = temp.next;
i++;
}
return temp;
}
//Function to reverse the linked list
public void reversePI ()
{
Node nm1 = null;
Node n = this.head;
Node np1 = n;
while (n.next != null)
{
np1 = np1.next;
n.next = nm1;
nm1 = n;
n = np1;
}
// Change next of last node
n.next = nm1;
// Swap head and tail
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
}
}
Output
Linked list before reversal
8 12 16 24
Linked list after reversal
24 16 12 8
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment