Learn

In a singly linked list, we can add to the head of the list by checking to see if it already has a head. We then either set the new node as the head (if there was no head) or update the head property, and link the past head to the new head.

Since a doubly linked list has an additional tail property and is built with nodes that each have two pointers, there are a few more steps:

  • Start by checking to see if there is a current head to the list
  • If there is (meaning the list is not empty), then we want to reset the pointers at the head of the list:
    • Set the current head’s previous node to the new head
    • Set the new head’s next node to the current head
  • Update the head property to be the new head
  • Finally, if there isn’t a current tail to the list (meaning the list was empty):
    • Update the tail property to be the new head since that node will be both the head and tail of the list

Instructions

1.

Define an .add_to_head() method that takes self and new_value as parameters.

Inside, create:

  • A new_head Node that takes new_value as a parameter
  • A current_head Node that’s set to the list’s head
2.

If there is a current head to the list:

  • Set current_head‘s previous node to new_head
  • Set new_head‘s next node to current_head

Remember to use the Node class’s .set_prev_node() and .set_next_node() methods.

3.

Outside of the if statement, set the list’s head to new_head.

4.

Lastly, if the list doesn’t have a tail, set the list’s tail to the new head.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?