Solution Review: Deletion by Value (Linked List)

from LinkedList import LinkedList
from Node import Node

def delete(lst, value):
    deleted = False
    if lst.is_empty():  # Check if list is empty -> Return False
        print("List is Empty")
        return deleted
    current_node = lst.get_head()  # Get current node
    previous_node = None  # Get previous node
    if is value:
        lst.delete_at_head()  # Use the previous function
        deleted = True
        return deleted

    # Traversing/Searching for Node to Delete
    while current_node is not None:
        # Node to delete is found
        if value is
            # previous node now points to next node
            previous_node.next_element = current_node.next_element
            current_node.next_element = None
            deleted = True
        previous_node = current_node
        current_node = current_node.next_element

    if deleted is False:
        print(str(value) + " is not in list!")
        print(str(value) + " deleted!")

    return deleted

lst = LinkedList()
delete(lst, 4)

In the above code, look at line no. 22 which says previous_node.next_element = current_node.next_element. Now, though I understand, this line will never get executed for first node since you have handled the condition of deleting first node above, this does not seem to be cleaner code as ideally “previous_node.next_element” cannot exists until it is assigned to current_node which happens only in line 26. Functionality wise this code will work, however, I feel one should avoid writing such a code where there is risk of accessing next_element for an object which is None.

Raising this as I got this as I got a comment on similar code one of my past interviews.