I can solve Step 24 on Build a Linked List. Step 24 Now, it's time to test removing an element from your linked list. At the bottom of your code, remove 1 from

class LinkedList:

class Node:

def \_*init*\_(self, element):

self.element = element

self.next = None

def \_*init*\_(self):

self.head = None

self.length = 0

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 and current_node.element != element:
        previous_node = current_node

        current_node = current_node.next

if current_node is None:

return

if previous_node:


        previous_node.next = current_node.next


else:

self.head = self.head.next

self.length -= 1

my_list = LinkedList()

my_list.add(1)

my_list.add(2)

my_list.remove(1)

print(my_list.length)

This is what I have but I keep getting getting You should print my_list.length.

hi, welcome to the forum

I tried to fix the formatting on your code as it was not displayed correctly when you posted.
I’m not a python coder though, so please check everything still looks the way you wanted it to look.

I dont know whats the issue here to be honest. I keep getting and error saying “You should print my_list.length.” Can someone please look into this.

please post again your code, there are some formatting issues that make it impossible to run

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

class LinkedList: class Node: def init(self, element): self.element = element self.next = None def init(self): self.head = None self.length = 0 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 and current_node.element != element: previous_node = current_node current_node = current_node.next if current_node is None: return if previous_node: previous_node.next = current_node.next else: self.head = self.head.next self.length -= 1 my_list = LinkedList() my_list.add(1) my_list.add(2) my_list.remove(1) print(my_list.length)

please don’t put it all on one line, it makes it not readable

Sorry about that.

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:
        previous_node = current_node
        current_node = current_node.next

    if current_node is None:
        return

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

    else:
        self.head = current_node.next

    self.length -= 1

my_list = LinkedList()

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

print(my_list.length)

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

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:
        previous_node = current_node
        current_node = current_node.next

    if current_node is None:
        return

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

    else:
        self.head = current_node.next

    self.length -= 1
my_list = LinkedList()

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

print(my_list.length)

Hello

Can you please link to the url of the step you are on.

It seems to me that you modified the original code, which causes the tests to fail. Please try resetting this step and type back what you added. Thank you.

I did that. It didn’t work tho.

Okay, please post your updated code.

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:
            previous_node = current_node
            current_node = current_node.next

        if current_node is None:
            return
        elif previous_node is not None:
            previous_node.next = current_node.next
        else:
            self.head = current_node.next

        self.length -= 1


# testing
my_list = LinkedList()

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

print(my_list.length)   

my_list.remove(1)       

print(my_list.length)

You should keep every single previous print statement, and only modify code in the highlighted section (I admit that the code interpreter is really strict).

Here, in the testing section, you have :

but it should look like this :

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)

#Your new code

thank you very much. Not it is solved.