Build a Linked List - Step 18

Tell us what’s happening:

the code is right but it show me that:1. You should have a while loop inside the remove method.
2. Your while loop should check for when current_node is not None and when current_node.element is not equal to element.
3. You should assign current_node to previous_node inside the while loop.
// tests completed
thank you

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

# User Editable Region


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      
    

# User Editable Region


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 browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build a Linked List - Step 18

Hi there,

Were you asked to write this line of code in this step?

Happy coding!

Thank you, yes I found the solution :folded_hands:t3:

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