0

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.

1
  • 1
    nodePtr = new Listnode(value); nodePtr-> next = head; head=nodePtr; Commented Oct 23, 2017 at 14:17

5 Answers 5

2

To create a new node to the head of a list, follow these steps

  1. Create a temporary node holding your value
  2. Set the temporary node's next to point to the head
  3. Set the head to temporary
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I thought something was missing. I even toyed with using a temporary node but I couldn't figure out the best way to use it. It was also around hour number 2 of being completely stumped so I'm sure that didn't help either.
Hey, it happens to the best of us and glad it helped. Review what to do when someone answered my question
1

You first have to create a new Node, then link it to the current head. Then you switch the reference from your previous head to the newly created node.

ListNode *newHead = new ListNode;
newHead->next = head;
head = newHead;

Comments

0

Think carefully about what you have and what you need to do:

  • You have two nodes that you care about: the current head and your new nodePtr.
  • You want nodePtr->next to point to your current head, and update head to point to nodePtr so that this is the first node in the list.

The order in which you do things is important: if you assign the new value of head before making nodePtr->next point to the previous value, you will lose your whole list! Therefore:

  1. Create your new node nodePtr.
  2. Make nodePtr->next point to head.
  3. Make head point to nodePtr

Comments

0

You are on the wrong way. The solution is create the new node, link the next node to head, and after refer the head to new node:

template <class T>
void LinkedList<T>::addBeg(T value)
{
    ListNode *nodePtr = new ListNode(value);
    nodePtr->next = head;
    head = nodePtr;
}

Comments

0

If you say nodePtr = head; then you're just overwriting the pointer you just made. You want to change nodePtr->next = head; head = nodePtr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.