... method, the method throws an NoSuchElementException (lines 67–69). Otherwise, the method returns a reference to the removed data. The steps are:
-
Assign
firstNode.data(the data being removed) toremovedItem(line 71). -
If
firstNodeandlastNoderefer to the same object (line 74), the list has only one element at this time. So, the method setsfirstNodeandlastNodetonull(line 75) to remove the node from the list (leaving the list empty). -
If the list has more than one node, then the method leaves reference
lastNodeas is and assigns the value offirstNode.nextNodetofirstNode(line 78). Thus,firstNodereferences the node that was previously the second node in the list. -
Return the
removedItemreference (line 81).
In Fig. 21.7, part (a) illustrates ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access