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()
Deleting the node with value 3 removes it from the list. The previous node (2) points to the next node (4), and the next node (4) points back to the previous node (2). The list becomes 1 -> 2 -> 4 -> 5 -> null.
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()
When deleting the head node (value 1), the head pointer updates to the next node (2). The previous pointer of node 2 becomes None. The list becomes 2 -> 3 -> null.
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()
The method returns silently if the value is not found. No error occurs. The list remains unchanged.
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()
Deleting the tail node (value 5) removes it. The previous node (4) now points to None. The list becomes 1 -> 2 -> 3 -> 4 -> null.
In a doubly linked list, each node has links to both previous and next nodes. When deleting a node, both links must be updated to keep the list connected correctly in both directions. Otherwise, traversing backward or forward may fail or cause errors.