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 encryptImage(ListNode* head)

{

// code here

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!