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
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
#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
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
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
- Deletion 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 –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- 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 –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Login/Signup to comment