Reverse a linked list without changing links between nodes (Data reverse only)

Reverse a linked list without changing links between nodes

 Reverse a linked list without changing links between nodes. What do you mean by that?

We have to change the data, in the same way we done it in a diagram we can say that we don’t have to change the links in between nodes we just have to change the matter or number over there with the other number in a linked list for the purpose of reversal. To make thing’s work we have to take two pointer’s that is left pointer and right pointer. Left pointer points to the 0th node and the right pointer points to the last node left node. In the continuation on this page we will learn about algorithm and a java program.

C program for reversing a linked list without changing the links between the nodes

Algorithm to reverse a linked list Data.

Step 1- Initialize a class.

Step 2- make head as null.

Step 3-assign tail=null also.

Step 4- A function to find the size of linked list i.e (this.size).

Step 5- Further check whether linked list is empty, (this.size() ==0).

Step 6- Traverse the node and print the linked list (node temp = head)

Step 7-print (temp.data).

Step  8- temp = temp.next

Step 9 – print (“End”).

Step 10- First add a node in at the beginning of  linked list.

Step 11- create a temp node which points towards head

Step 12- Then find if the linked list is empty.

Step 13- if it’s not then set the head such that it now points to temp node.

Step 14- Furthermore add a node at any index

Step 15- Throw new exception (“Index out of bound”)

Step 16- return temp

Step 17- take the left and right nodes to be swapped

int left=0

int right=this.size.

Step 18- swap left and right node data .

Step 19- Data will be reversed.

Java program to reverse a linked list without changing links between nodes

Java program to reverse a linked list without changing links between nodes

Run
import java.util.*;

public class Main
{

  public static void main (String[]args) 
  {
    try{
        LinkedList ll = new LinkedList ();
      ll.addFirst (2);
      ll.addFirst (4);
      ll.addFirst (6);
      ll.addFirst (8);

      ll.display ();

      ll.reverseDI ();
      ll.display ();
    }
     catch (Exception e) {
            System.out.println("Exception occurred.");
        }
  }

}

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 ("END");
  }

  // 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 reverseDI () throws Exception
  {

    // Take left and right to keep track of nodes to be swapped
    int left = 0;
    int right = this.size () ;

    while (left < right)
      {
    // Get left node
	Node leftNode = this.getNodeAt (left);
    // Get right node
	Node rightNode = this.getNodeAt (right);

    // Swap left and right node data
	int temp = leftNode.data;
	  leftNode.data = rightNode.data;
	  rightNode.data = temp;

      // Increment left and decrement right
	  left++;
	  right--;

      }

  }

}

Output

Linked list before reversal
2  4  6  8
Linked list after reversing the data
8  6  4  2

One comment on “Reverse a linked list without changing links between nodes (Data reverse only)”


  • Aakriti

    //in c//
    #include
    #include

    //reverse a linked list//
    struct node{
    int val;
    struct node* link;
    };

    struct node* insert(struct node* head,int val)
    {
    struct node* node=(struct node*)malloc(sizeof(struct node));

    node->val=head->val;
    node->link=head->link;
    head->val=val;
    head->link=node;

    return head;
    }

    void display(struct node* head)
    {
    struct node* nodes=head;
    while( nodes != NULL)
    {
    printf(“%d”,nodes->val);
    nodes=nodes->link;

    }
    }
    struct node* reverse(struct node* head)
    {
    struct node* temp=head;
    struct node *nodes;
    struct node* exp;
    int n=6;
    int i; //reverse the data of the linked list//
    for(i=1;i<=3;i++) //three times running
    {
    int j;
    nodes=temp;
    for(j=i;jlink;

    }
    exp->val=temp->val;
    temp->val=nodes->val;
    nodes->val=exp->val;
    –n;
    temp=temp->link;
    }
    return head;
    }
    int main()
    {
    struct node * head =(struct node*)malloc(sizeof(struct node));
    head->val=12;
    head->link=NULL;

    insert(head,34);
    insert(head,78);
    insert(head,56);
    insert(head,67);
    insert(head,60);

    reverse(head);
    display(head);

    }