Question
An image needs to be encrypted by replacing the value of the cell with the next greater value in the list. If no element in
An image needs to be encrypted by replacing the value of the cell with the next greater value in the list. If no element in the list is greater than the current element, encrypt the pixel value as 0. Write a function that takes as input the head of the linked list and returns a vector of the encrypted image.
Constraints
-
Length (list) > 2 and Length (list) % 3 == 0
-
Values in a linked list are between 1 and 256
Sample Input
6 3 1 6 4 7 2
Sample Output
6 6 7 7 0 0
Explanation
- Input: Line 1 denotes the length of the linked list. Line 2 denotes the elements in the linked list.
- Output: The elements in the returned vector. For the first element in the list, 3, we replace it with the next largest element in the list, i.e. 6 in this case and so on.
Given code
#include
#include
struct ListNode
{
int data;
ListNode* next;
};
ListNode* addEnd(ListNode* head, int value)
{
ListNode* newNode = new ListNode;
newNode -> data = value;
newNode -> next = nullptr;
if (head == nullptr)
return newNode;
ListNode* curr = head;
while(curr -> next != nullptr)
curr = curr -> next;
curr -> next = newNode;
return head;
}
std::vector
{
// code here
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started