📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Consider a delete_node(head, key) function for a singly

    linked list that removes the first occurrence of a node with a given key. The function works for most cases but throws a NullPointerException (or equivalent in other languages) when attempting to delete the last node in a list of two or more nodes. Which of the following is a common oversight that could lead to this specific bug?
    A Not handling the case where the head itself needs to be deleted. Correct Answer Incorrect Answer
    B Failing to update the next pointer of the *previous* node to None when the target node is the last. Correct Answer Incorrect Answer
    C Incorrectly traversing the list using current = current.next without checking current for None. Correct Answer Incorrect Answer
    D Not checking if the list is empty before starting the deletion process. Correct Answer Incorrect Answer
    E Forgetting to free the memory of the deleted node. Correct Answer Incorrect Answer

    Solution

    The correct answer is B

    Practice Next
    ask-question