# Modify contents of Linked List

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.
(No extra memory to be used).

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

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Approach: The following steps are:

1. Split the list from the middle. Perform front and back split. If the number of elements is odd, the extra element should go in the 1st(front) list.
2. Reverse the 2nd(back) list.
3. Perfrom the required subtraction while traversing both list simultaneously.
4. Again reverse the 2nd list.
5. Concatenate the 2nd list back to the end of the 1st list.
```// C++ implementation to modify the contents of
#include <bits/stdc++.h>
using namespace std;

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

/* function prototype for printing the list */
void printList(struct Node*);

/* Function to insert a node at the beginning of
void push(struct Node **head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

/* put in the data  */
new_node->data = new_data;

/* link the old list at the end of the new node */

/* move the head to point to the new node */
}

/* Split the nodes of the given list
into front and back halves,
and return the two lists
using the reference parameters.
Uses the fast/slow pointer strategy. */
struct Node **front_ref, struct Node **back_ref)
{
Node *slow, *fast;

/* Advance 'fast' two nodes, and
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}

/* 'slow' is before the midpoint in the list,
so split it in two at that point. */
*back_ref = slow->next;
slow->next = NULL;
}

/* Function to reverse the linked list */
{
struct Node *current, *prev, *next;
prev = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
}

// perfrom the required subtraction operation on
// the 1st half of the linked list
void modifyTheContentsOf1stHalf(struct Node *front,
struct Node *back)
{
// traversing both the lists simultaneously
while (back != NULL)
{
// subtraction operation and node data
// modification
front->data = front->data - back->data;

front = front->next;
back = back->next;
}
}

// function to concatenate the 2nd(back) list at the end of
// the 1st(front) list and returns the head of the new list
struct Node* concatFrontAndBackList(struct Node *front,
struct Node *back)
{

while (front->next != NULL)
front = front->next;

front->next    = back;

}

// function to modify the contents of the linked list
{
// if list is empty or contains only single node

struct Node *front, *back;

// split the list into two halves
// front and back lists

// reverse the 2nd(back) list
reverseList(&back);

// modify the contents of 1st half
modifyTheContentsOf1stHalf(front, back);

// agains reverse the 2nd(back) list
reverseList(&back);

// concatenating the 2nd list back to the
// end of the 1st list

// pointer to the modified list
}

// function to print the linked list
{
return;

{
cout << head->data << " -> ";
}
}

// Driver program to test above
int main()
{

// print the modified linked list
cout << "Modified List:" << endl;
return 0;
}
```

Output:

```Modified List:
-8 -> 2 -> -4 -> 12 -> 7 -> 10
```

Time Complexity: O(n), where n in the number of nodes.

This article is contributed by Ayush Jauhari. 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.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.6 Average Difficulty : 2.6/5.0
Based on 13 vote(s)