C Program to reverse each node value in Singly Linked List
A linked list is a linear collection of data elements, in which each node points to the next node. Unlike an array, it doesn’t have upper limit and hence extremely useful.
The task is to access value of each node of linked list and reverse them.
Examples:
Input : 56 87 12 49 35 Output :65 78 21 94 53 Input : 128 87 12433 491 33 Output :821 78 33421 194 33
Algorithm:
The task can be accomplished as:
- Linearly traverse each node of the singly linked list.
- Reverse the value of each node.
- Store the reversed value in the current node.
// C program to reverse every node data in // singly linked list. #include <stdio.h> #include <stdlib.h> // A linked list node struct Node { int data; struct Node* next; }; // newNode function inserts the new node at // the right side of linked list struct Node* newNode(int key) { struct Node* temp = new Node; temp->data = key; temp->next = NULL; return temp; } // reverse() will receive individual data item // from reverseIndividualData() and will return // the reversed number to calling function int reverse(int number) { int new_num = 0, rem; while (number != 0) { rem = number % 10; new_num = new_num * 10 + rem; number = number / 10; } return new_num; } void reverseIndividualData(struct Node* node) { if (node == NULL) return; while (node != NULL) { /* function call to reverse(), reverseIndividualData(struct Node *node) will send the one data item at a time to reverse(node->data) function which will return updated value to node->data*/ node->data = reverse(node->data); /* updating node pointer so as to get next value */ node = node->next; } } // Function to print nodes in linked list void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } // Driver program to test above functions int main() { struct Node* head = NULL; head = newNode(56); head->next = newNode(87); head->next->next = newNode(12); head->next->next->next = newNode(49); head->next->next->next->next = newNode(35); printf("\nList before reversing individual data item \n"); printList(head); reverseIndividualData(head); printf("\nList after reversing individual data item\n"); printList(head); return 0; } |
chevron_right
filter_none
Output:
List before reversing individual data item 56 87 12 49 35 List after reversing individual data item 65 78 21 94 53
Recommended Posts:
- Reverse alternate K nodes in a Singly Linked List
- Select a Random Node from a Singly Linked List
- Reverse alternate K nodes in a Singly Linked List - Iterative Solution
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Update adjacent nodes if the current node is zero in a Singly Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Recursive selection sort for singly linked list | Swapping node links
- Reverse each word in a linked list node
- Java Program for Reverse a linked list
- Program to reverse a linked list using Stack
- Program for n'th node from the end of a Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Create new linked list from two given linked list with greater element at each node
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



