Modify contents of Linked List – Recursive approach

Given a singly linked list containing n nodes. Modify the value of first half nodes such that 1st node’s new value is equal to the last node’s value minus first node’s current value, 2nd node’s new value is equal to the second last node’s value minus 2nd node’s current value, likewise for first half nodes. If n is odd then the value of the middle node remains unchanged.

Examples:

Input: 10 -> 4 -> 5 -> 3 -> 6
Output: -4 -> -1 -> 5 -> 3 -> 6

Input: 2 -> 9 -> 8 -> 12 -> 7 -> 10
Output: 8 -> -2 -> 4 -> 12 -> 7 -> 10



Approach: Traverse the linked list using recursion. So the traversed node gets stored in stack since we are using recursion (nodes will be in reverse order). Start traversing from the head and traverse till it reaches the half of the linked list and while traversing subtract the value of current node with the value of the node present in stack.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to modify the contents
// of the linked list with recursion
#include <bits/stdc++.h>
using namespace std;
  
// Represents a Node of the linked list
struct Node {
    int data;
    Node* next;
}* head = NULL, *ref_ptr = NULL;
  
// Function to create and return a new node
Node* insert(int data)
{
    Node* temp;
    temp = new Node;
    temp->data = data;
    temp->next = NULL;
    return temp;
}
  
  
// rotalNodesCount will have the count of
// total nodes present in the linked list
int totalNodesCount = 0, currentCount = 0;
  
// Function to modify the contents
// of the given linked lists
void modifyContents(Node* nod)
{
    if (nod == NULL) {
        return;
    }
  
    totalNodesCount++;
    modifyContents(nod->next);
    currentCount++;
  
    // If current node which the ref_ptr
    // is pointing to is in the first half
    // of the linked list
    if (currentCount <= (totalNodesCount / 2))
  
        // Update the current node
        ref_ptr->data = nod->data - ref_ptr->data;
  
    // Do not traverse the list further
    else
        return;
    ref_ptr = ref_ptr->next;
}
  
// Function to print the contents of the linked list
void print(Node* nod)
{
    if (nod == NULL) {
        return;
    }
    cout << nod->data;
    if (nod->next != NULL)
        cout << " -> ";
    print(nod->next);
}
  
// Driver code
int main()
{
    Node* ptr;
    head = insert(2);
    head->next = insert(9);
    head->next->next = insert(8);
    head->next->next->next = insert(12);
    head->next->next->next->next = insert(7);
    head->next->next->next->next->next = insert(10);
    ref_ptr = head;
    ptr = head;
  
    // Modify the linked list
    modifyContents(ptr);
  
    // Print the modified linked list
    print(ptr);
    return 0;
}

chevron_right


Output:

8 -> -2 -> 4 -> 12 -> 7 -> 10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.