... method, the method throws an NoSuchElementException (lines 67–69). Otherwise, the method returns a reference to the removed data. The steps are:

  1. Assign firstNode.data (the data being removed) to removedItem (line 71).

  2. If firstNode and lastNode refer to the same object (line 74), the list has only one element at this time. So, the method sets firstNode and lastNode to null (line 75) to remove the node from the list (leaving the list empty).

  3. If the list has more than one node, then the method leaves reference lastNode as is and assigns the value of firstNode.nextNode to firstNode (line 78). Thus, firstNode references the node that was previously the second node in the list.

  4. Return the removedItem reference (line 81).

In Fig. 21.7, part (a) illustrates ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.