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 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.

reverse a linked list

We have to change the data, in the same way we done it in a diagram we can say that  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.

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.

Reverse a linked list without changing links between nodes

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

Run
class LinkedList
{
  Node head;

  // Node Class
  class Node
  {
    int data;
    Node next;

      Node (int x)		// parameterized constructor
    {
      data = x;
      next = null;
    }
  }




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

  public Node insertBeginning (int data)
  {
    Node newNode = new Node (data);
    newNode.next = head;
    head = newNode;

    return head;
  }
  public void reverse ()
  {
    Node pointer = head;
    Node previous = null, current = null;

    while (pointer != null)
      {
	current = pointer;
	pointer = pointer.next;

	// reverse the link
	current.next = previous;
	previous = current;
	head = current;
      }

  }

}


 public class Main
{

  public static void main (String[]args)
  {
    try
    {
      LinkedList ll = new LinkedList ();
        ll.insertBeginning (2);
        ll.insertBeginning (4);
        ll.insertBeginning (6);
        ll.insertBeginning (8);
        
        System.out.println("LinkedList before reversal : ");

        ll.display ();
        
        System.out.println("LinkedList after reversal : ");

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

Output

LinkedList before reversal : 
8 6 4 2 END
LinkedList after reversal : 
2 4 6 8 END

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

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);

    }