BackmediumLinked Lists Accenture

Recursive Linked List Value Transformation Solution

Problem Statement

Given the `head` of a singly linked list and two integers, `X` and `Y`. Implement a function `transformList` that recursively modifies the `val` of each node in the list. For each node, if its `val` is strictly less than `Y`, increment `val` by `X`. Otherwise, decrement `val` by `X`. The function should return the `head` of the modified linked list.

Example 1
Input
head = 1 -> 5 -> 8 -> 2 -> NULL, X = 3, Y = 6
Output
4 -> 8 -> 5 -> 5 -> NULL

Explanation: Nodes with value < 6 are incremented by 3, others are decremented by 3. (1+3=4), (5+3=8), (8-3=5), (2+3=5).

Example 2
Input
head = 10 -> 20 -> 30 -> NULL, X = 5, Y = 0
Output
5 -> 15 -> 25 -> NULL

Explanation: Since all node values (10, 20, 30) are >= 0, each is decremented by 5. (10-5=5), (20-5=15), (30-5=25).

Constraints

  • 0 <= Number of nodes <= 10^4
  • -10^9 <= Node.val <= 10^9
  • -10^9 <= X <= 10^9
  • -10^9 <= Y <= 10^9
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free