Due to the added tail property, removing the head of the list in a doubly linked list is a little more complicated than doing so in a singly linked list:
- Start by checking if there’s a current head to the list.
- If there isn’t, the list is empty, so there’s nothing to remove and the method ends
- Otherwise, update the list’s head to be the current head’s next node
- If the updated head is not
None
(meaning the list had more than one element when we started):- Set the head’s previous node to
None
since there should be no node before the head of the list
- Set the head’s previous node to
- If the removed head was also the tail of the list (meaning there was only one element in the list):
- Call
.remove_tail()
to make the necessary changes to the tail of the list (we will create this method in the next exercise!)
- Call
- Finally, return the removed head’s value
Instructions
Define a .remove_head()
method that only takes self
as a parameter. Inside, create a removed_head
variable and set it to the list’s head node.
Check if removed_head
has no value. If so, that means there’s nothing to remove, so return None
to end the method.
Outside of your if
, set the list’s head to removed_head
‘s next node.
If the list still has a head, set its previous node to None
, since the head of the list shouldn’t have a previous node.
Check if removed_head
is equal to the list’s tail. If so, call the .remove_tail()
method (we will create this in the next exercise).
Finally, return removed_head
‘s value
.