University of Arizona, Department of Computer Science

CSc 120: Linked Lists

Expected Behavior

The code for the LinkedList and Node classes is provided here in the starter code for this problem. Note that this version of the linked list maintains a tail reference. That is, self._tail is an attribute of the linked list class and refers to the last node in the linked list.

Your task is to implement the method remove_last(self) for the LinkedList class with a tail reference.

Given a LinkedList object llist, the execution of the code

llist.remove_last()
returns the last element of llist and adjusts the tail reference to the second to the last element of the list, or None if the element removed was the last element of the list. If the list is empty, the method returns None.

Note: Normally you would expect CloudCoder to check that the element returned is the correct last element of the list. The last test case does check the value returned. However, for the other test cases, CloudCoder will verify that after the removal of the last element, the tail reference has been correctly adjusted.

Examples

  1. Call: remove_last(LList -> |6|:-> |4|:-> |2|:None; tail -> |2|:None)
    Result: "LList -> |6|:-> |4|:None; tail -> |4|:None"

  2. Call: remove_last(LList -> |6|:-> |4|:None; tail -> |4|:None)
    Result: "LList -> |6|:None; tail -> |6|:None"

  3. Call: remove_last(LList -> |6|:None; tail -> |6|:None)
    Result: "LList -> ; tail -> None"

  4. Call: remove_last(LList -> ; tail -> None)
    Result: None