Of course. Each call to insertFirst()
creates a new node,
links the new node in front of the current first node,
points headPtr
to the new node.
A third node is added to the list:
public class LinkedList { private Node headPtr = ; . . . public void insertFirst( int data ) { Node newFirst = new Node( data ); // 1 newFirst.setNext( headPtr ); // 2 headPtr = newFirst; // 3 } . . . }
The statement at ( // 1 ) creates a new Node
:
At ( // 2 ) the value in headPtr
is copied into the new Node
.
Finally ( // 3 ) a reference to the new node is copied into
headPtr
, and the two-node list is complete:
Start at headPtr
. Can you follow the arrows to the last node on the list?