![]() |
VOOZH | about |
Given a Singly linked list. The task is to modify the value of the first half of nodes such that 1st node's new value is equal to the value of the last node minus the first node's current value, 2nd node's new value is equal to the second last node's value minus 2nd nodes current value, likewise for first half nodes, then replace the second half of nodes with the initial values of the first half of nodes (values before modifying the nodes).
Note: If the size of it is odd then the value of the middle node remains unchanged.
Examples:
Input: head: 10 -> 4 -> 5 -> 3 -> 6
Output: -4 -> -1 -> 5 -> 4 -> 10
👁 ImageExplanation: After modifying first half, list will be: -4 -> -1 -> 5 -> 3 -> 6
After modifying Second half also, list will be: -4 -> -1 -> 5 -> 4 -> 10Input: head: 2 -> 9 -> 8 -> 12 -> 7 -> 10
Output: 8 -> -2 -> 4 -> 8 -> 9 -> 2
Explanation: After modifying the linked list as required, we have a new linked list containing the elements as 8 -> -2 -> 4 -> 8 -> 9 -> 2.
The idea is to take out all elements from LinkedList add them into an array and apply the required operations:
- For each node in the first half of the list, its value is changed by subtracting it from the corresponding value in the second half of the list (from the end towards the center).
- After performing these modifications, the values in the first half of the array are swapped with the corresponding values in the second half on the array and then put back all elements into the LinkedList.
-4 -1 5 4 10
The idea is to handle the modification of a singly linked list by reversing its second half to allow traversal in both directions.
- First, find the middle of the list and reverse the second half starting from the node after the middle.
- Then, traverse the first half and the reversed second half simultaneously, updating the first half's node values by subtracting their values from the corresponding nodes in the second half and swapping them.
- After the traversal, reverse the modified second half again to restore its original order.
- Finally, reconnect the first and restored second halves to complete the list modification.
-4 -1 5 4 10