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:

`// 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*

*filter_none*

**Output:**

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

## Recommended Posts:

- Modify contents of Linked List
- Recursive Approach to find nth node from the end in the linked list
- Recursive approach for alternating split of Linked List
- How to write C functions that modify head pointer of a Linked List?
- A Programmer's approach of looking at Array vs. Linked List
- Construct a linked list from 2D matrix (Iterative Approach)
- Recursive insertion and traversal linked list
- Iterative approach for removing middle points in a linked list of line segements
- Search an element in a Linked List (Iterative and Recursive)
- Check if linked list is sorted (Iterative and Recursive)
- Find Length of a Linked List (Iterative and Recursive)
- Recursive function to delete k-th node from linked list
- Recursive selection sort for singly linked list | Swapping node links
- Count all sub-sequences having product <= K - Recursive approach
- Modify and Rearrange List

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.