Which of the following is the correct statement to insert newnode after the header node?
a. head->next=newnode;
b. newnode->next=head;
c. newnode->next=head->next; head->next=newnode;
d. head->next=newnode;newnode->next=head;
Answers
Answer:
b
Explanation:
newnode->next=head
newnode->next=head
newnode->next=head
newnode->next=head
Answer:
The correct answer to the given question is:
b. newnode->next=head;
Explanation:
A linked list's new node will be placed at the top.
Algorithm
Create a head pointer and set it to NULL in step 1.
2. With the provided data, create a new node.
3. Set the new node such that it points at the head node.
The new node should then be designated as the head node.
You can avoid dealing with some edge circumstances by using sentinel nodes.
The biggest is the null check: You never have to worry about seeing if head is null because you always know that there will be a node at the top of the list that you may insert nodes after. (Having a tail node is beneficial for similar reasons as well)
#SPJ3