Learn

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
  • 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!)
  • Finally, return the removed head’s value

Instructions

1.

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.

2.

Check if removed_head has no value. If so, that means there’s nothing to remove, so return None to end the method.

3.

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.

4.

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).

5.

Finally, return removed_head‘s value.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?