C++ linked list insertion at the end assistance. I know that this works and everything, but I can't seem to understand some pieces of the code. I wanted to ask if someone could go through a simple linked list like 6, 7, and 8 step by step using this code. I have tried working it on paper, but I keep messing up somewhere. What is most confusing me is:
{ while(p->next != NULL)
p = p->next;
p->next = newNode;
}
return head;
Thank you.
Node* insertEnd(Node * head, int data){
Node * p = head;
Node * newNode = new Node;
newNode->data = data;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
while(p->next != NULL)
p = p->next;
p->next = newNode;
}
return head;
}
Get Answers For Free
Most questions answered within 1 hours.