C Program to reverse each node value in Singly Linked List

In this article, we are given a singly linked list. Our task is to create a C program to reverse each node value in the linked list − meaning if a node contains 123, it should become 321.

A singly linked list is a linear data structure where each node contains data and a pointer to the next node in the sequence.

Problem Example

Input: 34 → 12 → 89 → 56 → 72

Output: 43 → 21 → 98 → 65 → 27

Approach

To solve this problem, we will:

  1. Traverse each node in the singly linked list
  2. For each node, reverse its integer value using the digit reversal algorithm
  3. Update the node's data with the reversed value

Algorithm for Reversing Integer Value

To reverse an integer, we extract digits from right to left and build the reversed number ?

int reverseValue(int number) {
    int revElement = 0, rem;
    while (number != 0) {
        rem = number % 10;           // Extract last digit
        revElement = revElement * 10 + rem;  // Build reversed number
        number = number / 10;        // Remove last digit
    }
    return revElement;
}

Complete Program

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

struct Node {
    int data;
    struct Node* next;
};

struct Node* insertNode(int key) {
    struct Node* temp = (struct Node*)malloc(sizeof(struct 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("Original Linked List: ");
    printLinkedList(head);
    
    reverseLinkedListElements(head);
    
    printf("\nReversed Linked List: ");
    printLinkedList(head);
    
    return 0;
}
Original Linked List: 34 12 89 56 72 
Reversed Linked List: 43 21 98 65 27 

Key Points

  • The algorithm has O(n * m) time complexity, where n is the number of nodes and m is the average number of digits per node value.
  • Space complexity is O(1) as we reverse values in place.
  • The linked list structure remains unchanged − only the data values are modified.

Conclusion

This program successfully reverses each node value in a singly linked list by traversing each node and applying digit reversal. The approach is efficient and modifies values in place without changing the list structure.

Updated on: 2026-03-15T12:53:07+05:30

478 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements