Question: Write a method Node* add(Node* head, int index, int value) to add a node to a linked list. The head of the linked list is
Write a method Node* add(Node* head, int index, int value) to add a node to a linked list. The head of the linked list is input, as well as the index where the node should be added and the value associated with the node. The program returns the head of the updated list. If the index is greater than the size of the list, the program should return NULL.
We have defined the following node C++ class for you:
class Node { public: int value; Node* next = NULL; }; The program input is a value, an index, and a list. The output is a traversal of the updated list.
Sample Input 1:
5 1 0->1->2->3
Sample Output 1:
0->5->1->2->3
Sample Input 2:
5 0 0->1->2->3
Sample Output 2:
5->0->1->2->3
Sample Input 3:
5 4 0->1->2->3
Sample Output 3:
0->1->2->3->5
Sample Input 4:
5 5 0->1->2->3
Sample Output 4:
NULL
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
