go to previous page   go to home page   go to next page highlighting

Answer:

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.


Third 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:

Step One of Insert First

At ( // 2 ) the value in headPtr is copied into the new Node.

Step two of Insert First

Finally ( // 3 ) a reference to the new node is copied into headPtr, and the two-node list is complete:

Step three of Insert First

QUESTION 6:

Start at headPtr. Can you follow the arrows to the last node on the list?


go to previous page   go to home page   go to next page