Bird
Raised Fist0
DSA Pythonprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the operation Delete Node by Value do in a linked list?
easy
A. Removes the first node that contains the given value
B. Adds a new node with the given value at the end
C. Finds the node with the given value without removing it
D. Reverses the linked list

Solution

  1. Step 1: Understand the operation

    Deleting a node by value means removing the first node that matches the given value from the linked list.
  2. Step 2: Compare options

    Only Removes the first node that contains the given value describes removing the first node with the given value. Others describe different operations.
  3. Final Answer:

    Removes the first node that contains the given value -> Option A
  4. Quick Check:

    Delete Node by Value = Remove first matching node [OK]
Hint: Delete node by value removes first matching node [OK]
Common Mistakes:
  • Confusing deletion by value with adding a node
  • Thinking it deletes all nodes with the value
  • Mixing deletion by value with reversing the list
2. Which of the following is the correct Python syntax to delete a node by value from a singly linked list?
easy
A. def delete_node(head, value): current = head while current: if current.val == value: current = current.next break current = current.next
B. def delete_node(head, value): prev = None current = head while current: if current.val == value: if prev: prev.next = current.next else: head = current.next break prev = current current = current.next return head
C. def delete_node(head, value): for node in head: if node.val == value: head.remove(node) break return head
D. def delete_node(head, value): head = [x for x in head if x != value] return head

Solution

  1. Step 1: Analyze the deletion logic

    Deleting a node requires tracking the previous node to update its next pointer. def delete_node(head, value): prev = None current = head while current: if current.val == value: if prev: prev.next = current.next else: head = current.next break prev = current current = current.next return head correctly uses prev and current pointers and updates links properly.
  2. Step 2: Check other options

    def delete_node(head, value): current = head while current: if current.val == value: current = current.next break current = current.next only moves current pointer without changing links. def delete_node(head, value): for node in head: if node.val == value: head.remove(node) break return head uses invalid iteration on linked list nodes. def delete_node(head, value): head = [x for x in head if x != value] return head treats linked list as a list, which is incorrect.
  3. Final Answer:

    Option B code correctly deletes node by value in a singly linked list -> Option B
  4. Quick Check:

    Proper pointer update = def delete_node(head, value): prev = None current = head while current: if current.val == value: if prev: prev.next = current.next else: head = current.next break prev = current current = current.next return head [OK]
Hint: Use prev and current pointers to update links [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Trying to iterate linked list like a Python list
  • Not returning updated head after deletion
3. What will be the output linked list after deleting value 3 from the list: 1 -> 3 -> 5 -> 3 -> 7 -> null?
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def delete_node(head, value):
    prev = None
    current = head
    while current:
        if current.val == value:
            if prev:
                prev.next = current.next
            else:
                head = current.next
            break
        prev = current
        current = current.next
    return head

# Create list
head = Node(1)
head.next = Node(3)
head.next.next = Node(5)
head.next.next.next = Node(3)
head.next.next.next.next = Node(7)

head = delete_node(head, 3)

# Print list
current = head
result = []
while current:
    result.append(current.val)
    current = current.next
print(result)
medium
A. [1, 5, 3, 7]
B. [1, 3, 5, 7]
C. [3, 5, 3, 7]
D. [1, 5, 7]

Solution

  1. Step 1: Identify the first node with value 3

    The list is 1 -> 3 -> 5 -> 3 -> 7. The first node with value 3 is the second node.
  2. Step 2: Delete the first 3 and link previous node to next

    After deletion, the list becomes 1 -> 5 -> 3 -> 7.
  3. Final Answer:

    [1, 5, 3, 7] -> Option A
  4. Quick Check:

    First 3 removed, rest unchanged = [1, 5, 3, 7] [OK]
Hint: Delete only first matching node, keep rest intact [OK]
Common Mistakes:
  • Deleting all nodes with value 3 instead of first only
  • Not updating head when first node matches
  • Skipping nodes after deletion
4. The following code tries to delete a node by value from a linked list but causes an error. What is the error?
def delete_node(head, value):
    current = head
    while current.next:
        if current.next.val == value:
            current.next = current.next.next
            break
        current = current.next
    return head
medium
A. Fails when the value is not found
B. Fails when the list is empty
C. Fails when the node to delete is the last node
D. Fails when the node to delete is the head node

Solution

  1. Step 1: Analyze deletion logic

    The code checks current.next.val to find the node to delete, so it cannot delete the head node if it matches the value.
  2. Step 2: Check edge cases

    If the head node has the value, the code never deletes it because it only looks at current.next.
  3. Final Answer:

    Fails when the node to delete is the head node -> Option D
  4. Quick Check:

    Head node deletion not handled = Fails when the node to delete is the head node [OK]
Hint: Check if head node matches before loop [OK]
Common Mistakes:
  • Not handling deletion of head node separately
  • Assuming current.next always exists without check
  • Ignoring empty list case
5. You have a singly linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null. You want to delete the node with value 10 and then insert a new node with value 15 after the node with value 20. What will be the final linked list? Choose the correct sequence of operations and final list. Options: A) Delete 10, insert 15 after 20: 20 -> 15 -> 30 -> 40 -> 50 B) Delete 10, insert 15 after 20: 10 -> 20 -> 15 -> 30 -> 40 -> 50 C) Delete 10, insert 15 after 20: 20 -> 30 -> 15 -> 40 -> 50 D) Delete 10, insert 15 after 20: 15 -> 20 -> 30 -> 40 -> 50
hard
A. 20 -> 30 -> 15 -> 40 -> 50
B. 10 -> 20 -> 15 -> 30 -> 40 -> 50
C. 20 -> 15 -> 30 -> 40 -> 50
D. 15 -> 20 -> 30 -> 40 -> 50

Solution

  1. Step 1: Delete node with value 10

    Since 10 is the head, deleting it makes 20 the new head. List becomes 20 -> 30 -> 40 -> 50.
  2. Step 2: Insert node with value 15 after node with value 20

    Find node 20, insert 15 after it. List becomes 20 -> 15 -> 30 -> 40 -> 50.
  3. Final Answer:

    20 -> 15 -> 30 -> 40 -> 50 -> Option C
  4. Quick Check:

    Delete head 10, insert 15 after 20 = 20 -> 15 -> 30 -> 40 -> 50 [OK]
Hint: Delete head carefully, then insert after target node [OK]
Common Mistakes:
  • Not updating head after deleting first node
  • Inserting new node in wrong position
  • Confusing node values and positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard