Build a Linked List - Step 21

i stuck in step 21 and it show you should assign current_node.next to previous_node.next inside the elif statement.

def __init__(self):
    self.length = 0
    self.head = None

def is_empty(self):
    return self.length == 0

def add(self, element):
    node = self.Node(element)

    if self.is_empty():
        self.head = node
    else:
        current_node = self.head
        while current_node.next is not None:

Your code so far

class LinkedList:
    class Node:
        def __init__(self, element):
            self.element = element
            self.next = None

    def __init__(self):
        self.length = 0
        self.head = None

    def is_empty(self):
        return self.length == 0

    def add(self, element):
        node = self.Node(element)

        if self.is_empty():
            self.head = node
        else:
            current_node = self.head
            while current_node.next is not None:
                current_node = current_node.next
            current_node.next = node

        self.length += 1

    def remove(self, element):
        previous_node = None
        current_node = self.head

        while current_node is not None and current_node.element != element:

# User Editable Region

            previous_node = current_node
            current_node = current_node.next

        if previous_node is None:
            self.head = current_node.next

        elif previous_node is not None:
            previous_node.next = current_node.next

# User Editable Region


        self.length -= 1


my_list = LinkedList()

print(my_list.is_empty())

my_list.add(1)
my_list.add(2)

print(my_list.is_empty())
print(my_list.length)

my_list.remove(1)

print(my_list.length)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build a Linked List - Step 21

Please help me to find the error in my code

Welcome to the forum @amit91vishwakarma !

You seem to have replaced the starting code with this code, which came from where??

Please click the reset button to restore the original code and try again.

image

Happy coding!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.