Reverse a linked list by changing links between nodes

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

 

reverse a Linked List in java

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

Reverse a linked list by changing links between nodes

Java code to reverse a linked list by changing links between the nodes

Run
import java.util.*;

 class Main
{

  public static void main (String[]args) throws Exception
  {
    LinkedList ll = new LinkedList ();
      ll.addFirst (24);
      ll.addFirst (16);
      ll.addFirst (12);
      ll.addFirst (8);
	  
      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

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java