C Program for Reversing a linked list by changing links between the nodes

C Program for Reversing a linked list by changing links between the nodes

Reverse a Linked List by changing the links between the nodes

Here we will learn how to code a C program for reversing a linked list by changing the links between the nodes. That is, in this program we will be reversing the pointers of the node. i.e, if the user entered 1,2,3,4,5 in the linked list, the linked list will be reversed and the output will be 5,4,3,2,1. Let’s see how to code a C program for this problem

How to reverse a Linked List by changing the links between the nodes

  • Step 1 – We will take three pointers ptr1, ptr2 and ptr3. Initially the first and the third pointer will point NULL, and  the second pointer will point the head.
  • Step  2 – We will iterate through the linked list, swapping the value of these 3 pointers.
  • Step 3 – ptr3 will now point the second node of the linked, and ptr2 will be removed from the linked list.
  • Step 4 – Now ptr1 will point the removed node.
  • Step 5 – ptr2 node will point ptr1 node.
  • Step 6 – All the three pointers will be moved by +1.
  • Step 7 – The above steps will be repeated until ptr3 points the NULL.
  • Step 8 – Than in the last step, the last node will become the HEAD of the linked list
C Program for Reversing a Linked List without changing the links between the nodes-1

Algorithm for reversing the data of a linked list

  • START WHILE
  • NEXT=CURRENT->NEXTPTR
  • CURRENT->NEXTPTR=PREV
  • PREV=CURRENT
  • CURRENT=NEXT
  • END WHEN CURRENT=NULL
  • HEAD_ADDR=PREV

C Program for Reversing a Linked List, by changing links between the nodes

Run
#include<stdio.h>
#include<stdlib.h>

/* Link list node */
struct Node
{
  int data;
  struct Node *next;
};

/* Function to reverse the linked list */
void reverseLinkedList (struct Node **head_addr)
{
  struct Node *prev = NULL;
  struct Node *current = *head_addr;
  struct Node *next = NULL;
  while (current != NULL)
    {
      // Store next 
      next = current->next;

      // Reverse current node's pointer 
      current->next = prev;

      // Move pointers one position ahead. 
      prev = current;
      current = next;
    }
  *head_addr = prev;
}

/* Function to push a node */
void insert (struct Node **head_addr, int new_data)
{
  struct Node *new_node = (struct Node *) malloc (sizeof (struct Node));
  new_node->data = new_data;
  new_node->next = (*head_addr);
  (*head_addr) = new_node;
}

/* Function to print linked list */
void display (struct Node *head)
{
  struct Node *temp = head;
  while (temp != NULL)
    {
      printf ("%d  ", temp->data);
      temp = temp->next;
    }
}

int main ()
{
  int i, n, data;
  /* Start with the empty list */
  struct Node *head = NULL;
  printf ("Enter the number of nodes: ");
  scanf ("%d", &n);
  printf ("\nEnter the data in the nodes: ");
  for (i = 0; i < n; i++)
    {
      scanf ("%d", &data);
      insert (&head, data);
    }

  printf ("\nGiven linked list: ");
  reverseLinkedList (&head);
  display (head);

  printf ("\n\nReversed Linked list: ");
  reverseLinkedList (&head);
  display (head);

  return 0;
}

Output

Enter the number of nodes: 5

Enter the data in the nodes: 1 2 3 4 5

Given linked list: 1 2 3 4 5

Reversed Linked list: 5 4 3 2 1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Quiz time

Fun Fact

Problems for performing different operations on Linked List are asked in various coding competitions, either directly or in the form of a logical problem. Here we have described one of the manner of reversing a Linked list, but there are few more ways of reversing a linked list according to your needs, like Reversing it without changing the links, or just reverse printing the data.

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

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