Write a C++ function to change the value of a node at the beginning of a linked
ID: 3591283 • Letter: W
Question
Write a C++ function to change the value of a node at the beginning of a linked list.
Your function takes two arguments - the head of the linked list and the value num to which you change the head node's value. The list has at least one value in it.
Your function should modify the head of the linked list to have the new value.
Example:
Initial List: 4->2->3, key = 5
List After Function Call: 5->2->3
The linked list structure:
For example:
Test Result// Linked list before function call: 5->3->2->4 // ChangeValue(head, 2); // Linked list after function call: 2->3->2->4
After calling your function, the list is 2->3->2->4
Explanation / Answer
Here is the code for you:
struct node
{
int key;
node *next;
};
void ChangeValue(node*& head, int key)
{
//First lets see if there is atleast one node, if not, you can't make this happen.
if(head != NULL)
{
//lets modify the first node key value to this newly passed key value.
head->key = key;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.