Izekar
JCHJava Community | Help. Code. Learn.
•Created by Izekar on 11/30/2024 in #java-help
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
5 replies