0
0
DSA Pythonprogramming~20 mins

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

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