FAQ: Doubly Linked Lists: Python - Removing the Tail

This community-built FAQ covers the “Removing the Tail” exercise from the lesson “Doubly Linked Lists: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Pass the Technical Interview with Python

FAQs on the exercise Removing the Tail

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

When the list has one element, it means the head node and the tail node of the list are the same. Both methods, remove_tail() and remove_head() call each other, and it ends up in an infinite loop. Am I missing something, or this is the expected behavior?

I get a recursion error. Is there something wrong about my code? Is this the infinite loop the other poster pointed out?

class Node:

    def __init__(self, value, prev_node=None, next_node=None):

        self.value = value

        self.prev_node = prev_node

        self.next_node = next_node

    def set_next_node(self, next_node):

        self.next_node = next_node

    def set_prev_node(self, prev_node):

        self.prev_node = prev_node

    def get_next_node(self):

        return self.next_node

    def get_prev_node(self):

        return self.prev_node

    def get_value(self):

        return self.value

class DoublyLinkedList:

    def __init__(self):

        self.head_node = None

        self.tail_node = None

    def add_to_head(self, new_value):

        new_head = Node(new_value)

        current_head = self.head_node

        if current_head is not None:

            current_head.set_prev_node(new_head)

            new_head.set_next_node(current_head)

        self.head_node = new_head

        if self.tail_node is None:

            self.tail_node = new_head

    def add_to_tail(self, new_value):

        new_tail = Node(new_value)

        current_tail = self.tail_node

        if current_tail is not None:

            current_tail.set_next_node(new_tail)

            new_tail.set_prev_node(current_tail)

        self.tail_node = new_tail

        if self.head_node is None:

            self.head_node = new_tail

    def remove_head(self):

        removed_head = self.head_node

        new_head = removed_head.next_node

        if removed_head == None:

            return None

        if new_head != None:

            new_head.set_prev_node(None)

            self.head_node = new_head

        if removed_head == self.tail_node:

            self.remove_tail()

        return removed_head.get_value()

    def remove_tail(self):

        removed_tail = self.tail_node

        new_tail = self.tail_node.prev_node

        if removed_tail == None:

            return None

        if new_tail != None:

            new_tail.set_next_node(None)

            self.tail_node = new_tail

        if removed_tail == self.head_node:

            self.remove_head()

        return removed_tail.get_value()

It should not end up in an infinite loop bcz if there is only one node, the tail node and head node will each be set to None over the course of remove_head() and remove_tail(), respectively.