
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to reverse each node value in Singly Linked List
In this article, we are given a linked list. Our task is to create a C program to reverse each node value in singly linked list.
We will take each node of the linked list and reverse the value.
Linked list is a sequence of links that contains items that are linked to another link.
Let’s take an example to understand the problem,
Input
34 12 89 56 72
Output
43 21 98 65 27
To solve this problem, we will traverse the singly linked list and take each node. And then reverse the value of the current node.
Program to reverse each node value in Singly Linked List
// Program to reverse each node value in Singly Linked List.
Example
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* insertNode(int key) { struct Node* temp = new Node; temp->data = key; temp->next = NULL; return temp; } int reverseValue(int number) { int revElement = 0, rem; while (number != 0) { rem = number % 10; revElement = revElement * 10 + rem; number = number / 10; } return revElement; } void reverseLinkedListElements(struct Node* node) { if (node == NULL) return; while (node != NULL) { node->data = reverseValue(node->data); node = node->next; } } void printLinkedList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { struct Node* head = NULL; head = insertNode(34); head->next = insertNode(12); head->next->next = insertNode(89); head->next->next->next = insertNode(56); head->next->next->next->next = insertNode(72); printf("Orignal Linked List :\t"); printLinkedList(head); reverseLinkedListElements(head); printf("
Altered Linked List:\t"); printLinkedList(head); return 0; }
Output
Orignal Linked List : 34 12 89 56 72 Altered Linked List: 43 21 98 65 27
- Related Articles
- Reverse each word in a linked list Node
- C++ Program to Delete the First Node in a given Singly Linked List
- C++ Program to Implement Singly Linked List
- Program to find the middle node of a singly linked list in Python
- Reverse Alternate K Nodes in a Singly Linked List in C++
- C++ Program to Implement Circular Singly Linked List
- C++ Program to Implement Sorted Singly Linked List
- C# program to find node in Linked List
- C++ Program to Implement Sorted Circularly Singly Linked List
- Delete a tail node from the given singly Linked List using C++
- Write a program in C++ to insert a Node at the beginning of the given Singly linked list
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- Golang Program to define a singly linked list.
- Create new linked list from two given linked list with greater element at each node in C++ Program

Advertisements