0
0
DSA Pythonprogramming~20 mins

Delete Node by Value in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after deleting a node by value in a singly linked list
What is the printed state of the linked list after deleting the node with value 3?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, val):
        if not self.head:
            self.head = Node(val)
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = Node(val)

    def delete_by_value(self, val):
        current = self.head
        if current and current.val == val:
            self.head = current.next
            return
        prev = None
        while current and current.val != val:
            prev = current
            current = current.next
        if current:
            prev.next = current.next

    def print_list(self):
        current = self.head
        result = []
        while current:
            result.append(str(current.val))
            current = current.next
        print(" -> ".join(result) + " -> null")

ll = LinkedList()
for v in [1, 2, 3, 4, 5]:
    ll.append(v)
ll.delete_by_value(3)
ll.print_list()
A1 -> 2 -> 4 -> 5 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
C2 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 4 -> null
Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
Predict Output
intermediate
2:00remaining
Output after deleting the head node by value
What is the printed state of the linked list after deleting the node with value 1 (head node)?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, val):
        if not self.head:
            self.head = Node(val)
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = Node(val)

    def delete_by_value(self, val):
        current = self.head
        if current and current.val == val:
            self.head = current.next
            return
        prev = None
        while current and current.val != val:
            prev = current
            current = current.next
        if current:
            prev.next = current.next

    def print_list(self):
        current = self.head
        result = []
        while current:
            result.append(str(current.val))
            current = current.next
        print(" -> ".join(result) + " -> null")

ll = LinkedList()
for v in [1, 2, 3, 4, 5]:
    ll.append(v)
ll.delete_by_value(1)
ll.print_list()
A1 -> 2 -> 3 -> 4 -> 5 -> null
B2 -> 3 -> 4 -> 5 -> null
C1 -> 3 -> 4 -> 5 -> null
D2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Deleting the head node means the head pointer moves to the next node.
🔧 Debug
advanced
2:00remaining
Identify the error in delete_by_value method
What error will occur when deleting a node by value in the following code if the value is not found in the list?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def delete_by_value(self, val):
        current = self.head
        if current and current.val == val:
            self.head = current.next
            return
        prev = None
        while current and current.val != val:
            prev = current
            current = current.next
        prev.next = current.next

ll = LinkedList()
for v in [1, 2, 3]:
    ll.append(v)
ll.delete_by_value(4)
AAttributeError: 'NoneType' object has no attribute 'next'
BIndexError: list index out of range
CNo error, node is deleted successfully
DTypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Attempts:
2 left
💡 Hint
Consider what happens when the value is not found and current becomes None.
Predict Output
advanced
2:00remaining
Output after deleting the last node by value
What is the printed state of the linked list after deleting the node with value 5 (last node)?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, val):
        if not self.head:
            self.head = Node(val)
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = Node(val)

    def delete_by_value(self, val):
        current = self.head
        if current and current.val == val:
            self.head = current.next
            return
        prev = None
        while current and current.val != val:
            prev = current
            current = current.next
        if current:
            prev.next = current.next

    def print_list(self):
        current = self.head
        result = []
        while current:
            result.append(str(current.val))
            current = current.next
        print(" -> ".join(result) + " -> null")

ll = LinkedList()
for v in [1, 2, 3, 4, 5]:
    ll.append(v)
ll.delete_by_value(5)
ll.print_list()
A1 -> 2 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 3 -> null
C1 -> 2 -> 3 -> 4 -> null
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Deleting the last node means the previous node's next becomes None.
🧠 Conceptual
expert
2:00remaining
Why is it important to handle deletion of the head node separately?
In the delete_by_value method, why do we check if the head node contains the value before traversing the list?
ABecause the head node does not have a next pointer
BBecause the head node cannot be deleted in a singly linked list
CBecause the head node is always the last node in the list
DBecause deleting the head node requires updating the head pointer to the next node
Attempts:
2 left
💡 Hint
Think about what changes when the first node is removed.