Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Delete by Value in Doubly Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly 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 middle value in doubly linked list
What is the printed state of the doubly linked list after deleting the value 3?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

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

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_by_value(self, value):
        current = self.head
        while current:
            if current.data == value:
                if current.prev:
                    current.prev.next = current.next
                else:
                    self.head = current.next
                if current.next:
                    current.next.prev = current.prev
                return
            current = current.next

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

# Setup
dll = DoublyLinkedList()
dll.append(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.append(5)

# Delete value 3

dll.delete_by_value(3)

# Print list

dll.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 links of previous and next nodes are updated when deleting a middle node.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node by value
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

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

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_by_value(self, value):
        current = self.head
        while current:
            if current.data == value:
                if current.prev:
                    current.prev.next = current.next
                else:
                    self.head = current.next
                if current.next:
                    current.next.prev = current.prev
                return
            current = current.next

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

# Setup

dll = DoublyLinkedList()
dll.append(1)
dll.append(2)
dll.append(3)

# Delete value 1 (head)

dll.delete_by_value(1)

# Print list

dll.print_list()
A3 -> null
B2 -> 3 -> null
C2 -> null
D1 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Deleting the head means the head pointer moves to the next node.
🔧 Debug
advanced
2:00remaining
Identify the error in delete_by_value method
What error will this code raise when trying to delete a value not present in the list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

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

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_by_value(self, value):
        current = self.head
        while current:
            if current.data == value:
                if current.prev:
                    current.prev.next = current.next
                else:
                    self.head = current.next
                if current.next:
                    current.next.prev = current.prev
                return
            current = current.next
        # No return or error if value not found

# Setup

dll = DoublyLinkedList()
dll.append(1)
dll.append(2)
dll.append(3)

# Delete value 4 (not in list)

dll.delete_by_value(4)

# Print list

dll.print_list()
ANo error, list remains unchanged: 1 -> 2 -> 3 -> null
BTypeError due to NoneType assignment
CAttributeError because current becomes None and code tries to access current.prev
DIndexError because of invalid index access
Attempts:
2 left
💡 Hint
Check what happens when the value is not found in the list.
❓ Predict Output
advanced
2:00remaining
Output after deleting the tail node by value
What is the printed state of the doubly linked list after deleting the value 5 (tail node)?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

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

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_by_value(self, value):
        current = self.head
        while current:
            if current.data == value:
                if current.prev:
                    current.prev.next = current.next
                else:
                    self.head = current.next
                if current.next:
                    current.next.prev = current.prev
                return
            current = current.next

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

# Setup

dll = DoublyLinkedList()
dll.append(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.append(5)

# Delete value 5 (tail)

dll.delete_by_value(5)

# Print list

dll.print_list()
A1 -> 2 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 3 -> null
C1 -> 2 -> 3 -> 4 -> null
D2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Deleting the tail node means the previous node's next pointer becomes None.
🧠 Conceptual
expert
2:00remaining
Why is updating both prev and next pointers necessary when deleting a node?
In a doubly linked list, when deleting a node by value, why must both the previous node's next pointer and the next node's prev pointer be updated?
AIt is not necessary to update any pointers because garbage collection handles it
BOnly the previous node's next pointer needs updating because the next node's prev pointer is not used
CUpdating either pointer is enough because the other updates automatically
DTo maintain the bidirectional links so traversal in both directions remains correct
Attempts:
2 left
💡 Hint
Think about how nodes connect forward and backward in a doubly linked list.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. The list is reversed instead of deleting
B. All nodes are deleted regardless of value
C. Only the first node is deleted always
D. The node with the matching value is removed and links are updated

Solution

  1. Step 1: Locate the node with the given value

    Traverse the list from head to tail to find the node whose data matches the value to delete.
  2. Step 2: Update links to remove the node

    Adjust the previous node's next pointer and the next node's previous pointer to bypass the node to be deleted.
  3. Final Answer:

    The node with the matching value is removed and links are updated -> Option D
  4. Quick Check:

    Delete by value means remove matching node [OK]
Hint: Find node, update previous and next pointers [OK]
Common Mistakes:
  • Not updating both previous and next pointers
  • Deleting all nodes instead of one
  • Ignoring the case when node is head or tail
2. Which of the following is the correct syntax to delete a node by value in a doubly linked list in Python?
easy
A. def delete_by_value(self, value): current = self.head while current: if current.data == value: # update links break current = current.next
B. def delete_by_value(value): current = head while current: if current.data = value: # update links break current = current.next
C. def delete_by_value(self, value): current = self.head while current: if current.data == value # update links break current = current.next
D. def delete_by_value(self, value): current = self.head while current: if current.data == value: # update links continue current = current.next

Solution

  1. Step 1: Check method signature and loop

    def delete_by_value(self, value): current = self.head while current: if current.data == value: # update links break current = current.next correctly defines a method with self and value, and uses a while loop to traverse nodes.
  2. Step 2: Verify condition and syntax

    def delete_by_value(self, value): current = self.head while current: if current.data == value: # update links break current = current.next uses '==' for comparison and breaks after deletion logic, which is correct syntax and logic.
  3. Final Answer:

    Option A has correct syntax and logic for deletion by value -> Option A
  4. Quick Check:

    Correct method and loop syntax [OK]
Hint: Use '==' for comparison and break after deletion [OK]
Common Mistakes:
  • Using '=' instead of '==' in condition
  • Missing colon ':' after if statement
  • Not including self parameter in method
3. What will be the output of the following code after deleting value 20?
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

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

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_by_value(self, value):
        current = self.head
        while current:
            if current.data == value:
                if current.prev:
                    current.prev.next = current.next
                else:
                    self.head = current.next
                if current.next:
                    current.next.prev = current.prev
                break
            current = current.next

    def print_list(self):
        current = self.head
        result = []
        while current:
            result.append(current.data)
            current = current.next
        print(result)

dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.append(30)
dll.delete_by_value(20)
dll.print_list()
medium
A. [10]
B. [10, 20, 30]
C. [10, 30]
D. [20, 30]

Solution

  1. Step 1: Append nodes 10, 20, 30

    The list becomes 10 <-> 20 <-> 30.
  2. Step 2: Delete node with value 20

    Node 20 is found and removed by updating 10's next to 30 and 30's prev to 10.
  3. Final Answer:

    [10, 30] -> Option C
  4. Quick Check:

    Deleted 20, list is [10, 30] [OK]
Hint: After deletion, list skips removed value [OK]
Common Mistakes:
  • Not updating head when deleting first node
  • Not updating prev pointer of next node
  • Printing list before deletion
4. Identify the error in the following delete_by_value method for a doubly linked list:
def delete_by_value(self, value):
    current = self.head
    while current:
        if current.data == value:
            current.prev.next = current.next
            current.next.prev = current.prev
            break
        current = current.next
medium
A. Does not traverse the list correctly
B. Does not handle deletion when current is head (current.prev is None)
C. Uses break instead of continue after deletion
D. Deletes all nodes with the value instead of one

Solution

  1. Step 1: Check handling of head node deletion

    If current is head, current.prev is None, so current.prev.next causes error.
  2. Step 2: Confirm missing condition for head update

    Code does not update self.head when deleting the first node, causing attribute error.
  3. Final Answer:

    Does not handle deletion when current is head (current.prev is None) -> Option B
  4. Quick Check:

    Head deletion needs special handling [OK]
Hint: Check if node is head before updating prev pointer [OK]
Common Mistakes:
  • Not checking if current.prev is None
  • Assuming all nodes have prev and next
  • Forgetting to update head pointer
5. Given a doubly linked list with nodes 5 <-> 10 <-> 15 <-> 20, what will be the list after deleting value 5 and then value 20?
hard
A. [10, 15]
B. [5, 10, 15, 20]
C. [15]
D. [10, 15, 20]

Solution

  1. Step 1: Delete node with value 5 (head)

    After deleting 5, head moves to 10, list is 10 <-> 15 <-> 20.
  2. Step 2: Delete node with value 20 (tail)

    After deleting 20, 15 becomes tail, list is 10 <-> 15.
  3. Final Answer:

    [10, 15] -> Option A
  4. Quick Check:

    Deleted head and tail nodes correctly [OK]
Hint: Delete head and tail carefully, update pointers [OK]
Common Mistakes:
  • Not updating head after deleting first node
  • Not updating tail after deleting last node
  • Deleting nodes in wrong order
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