Open In App

C Program to reverse each node value in Singly Linked List

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Linearly traverse each node of the singly linked list.
  2. Reverse the value of each node.
  3. Store the reversed value in the current node.

Implementation:

C




// 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
        = (struct Node*)malloc(sizeof(struct 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;
}


Output

List before reversing individual data item 
56 87 12 49 35 
List after reversing individual data item
65 78 21 94 53 


Similar Reads

Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
Convert Singly Linked List to XOR Linked List
Prerequisite: XOR Linked List – A Memory Efficient Doubly Linked List | Set 1XOR Linked List – A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a singly linked list, the task is to convert the gi
9 min read
When is Doubly Linked List more Efficient than Singly Linked List?
Did you know there are some cases where a Doubly Linked List is more efficient than a Singly Linked List, even though it takes more memory compared to a Singly Linked List? What are those Cases? Well, we will discuss that in the following article, But first, let's talk about Singly and linked lists: What is a Singly Linked List?A singly linked list
4 min read
Convert singly linked list into circular linked list
Given a singly linked list, we have to convert it into circular linked list. For example, we have been given a singly linked list with four nodes and we want to convert this singly linked list into circular linked list. Approach: The idea is to traverse the singly linked list and check if the node is the last node or not. If the node is the last no
14 min read
Reverse alternate K nodes in a Singly Linked List
Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm. Example: Inputs: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;9-&gt;NULL and k = 3 Output: 3-&gt;2-&gt;1-&gt;4-&gt;5-&gt;6-&gt;9-&gt;8-&gt;7-&gt;NULL. Method 1 (Process 2k node
22 min read
Reverse alternate K nodes in a Singly Linked List - Iterative Solution
Given a linked list and an integer K, the task is to reverse every alternate K nodes.Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; 9 -&gt; NULL, K = 3 Output: 3 2 1 4 5 6 9 8 7Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; 9 -&gt; NULL, K = 5 Output: 5 4 3 2 1 6 7 8 9 Approach: We have alre
12 min read
Reverse a singly Linked List in groups of given size | Set 3
Given a singly linked list and an integer K, the task is to reverse every K nodes of the given linked list. Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; NULL, K = 3 Output: 3 2 1 6 5 4 8 7 Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; NULL, K = 5 Output: 5 4 3 2 1 8 7 6 Approach: Two diff
11 min read
Reverse a singly Linked List in groups of given size | Set 4 (Space efficient approach)
Given a linked list, write a function to reverse every k node (where k is an input to the function). Examples: Inputs: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL, k = 3Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL Inputs: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL, k = 5Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-&gt;8-&gt;
9 min read
C++ Program For Selecting A Random Node From A Singly Linked List
Given a singly linked list, select a random node from the linked list (the probability of picking a node should be 1/N if there are N nodes in the list). You are given a random number generator.Below is a Simple Solution: Count the number of nodes by traversing the list.Traverse the list again and select every node with probability 1/N. The selecti
5 min read
C Program For Selecting A Random Node From A Singly Linked List
Given a singly linked list, select a random node from the linked list (the probability of picking a node should be 1/N if there are N nodes in the list). You are given a random number generator.Below is a Simple Solution: Count the number of nodes by traversing the list.Traverse the list again and select every node with probability 1/N. The selecti
4 min read
Article Tags :
Practice Tags :