I'm struggling with how to link the new node I am creating to the rest of my linked list.
template <class T>
void LinkedList<T>::addBeg(T value)
{
ListNode *nodePtr = head;
head = nodePtr->next;
head = nodePtr;
nodePtr = new ListNode(value);
}
I know what I did wrong here: the new value is not associated the linked list at all.
I think I know what I need to do. I'm pretty sure what I need to do is create the new value, insert into the beginning of the existing list, and then redefine head to be the newly created value.
The problem I'm having, is I don't know how to do this.
So, what I think I need to do (at least logically), is set
*nodePtr = new Listnode(value);
Then set
nodePtr = head;
then set
head = nodePtr-> next;
and then set the
new ListNode(value) = head;
Am I on the right track? I can't shake the nagging feeling that I'm not correctly linking the new ListNode to the existing list and I can't figure out if I am making the wrong steps or if I'm missing a step.
nodePtr = new Listnode(value);nodePtr-> next = head;head=nodePtr;