Question
Add a node to a linked list Write a program add(Node* head, int index, int value) to add a node to a linked list. The
Add a node to a linked list
Write a program 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
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