Java Community | Help. Code. Learn.JC|HCL
Java Community | Help. Code. Learn.โ€ข15mo agoโ€ข
4 replies
Izekar

Can someone please explain to me how this works

public class LinkedList
{
. . .
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
}
. . .
}

I know that newNode sets data = element passed in the parameter
but why does the next which is supposed to point to the next node, point to the first node?
And why is first = newNode? Isn't newNode's .next() pointing to first? wouldn't it point to itself that way? What am I missing please help
Was this page helpful?