Read Time:3 Minute, 42 Second
Deleting a node from a doubly linked list
Deleting a node from the beginning of the list
C Code
void deleteAtBegin() {
if(head == NULL) {
printf("List is empty, no deletion can be performed.");
return;
}
struct node* temp = head;
head = head->next;
head->prev = NULL;
free(temp);
}
Java Code
class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
Node head;
public void deleteAtStart() {
if(head == null) {
System.out.println("List is empty");
return;
}
//update the head pointer to point to the next node
head = head.next;
head.prev = null;
}
}
Python code
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def deleteAtStart(self):
if self.head is None:
print("List is empty")
return
#update the head pointer to point to the next node
self.head = self.head.next
self.head.prev = None
deleting a node from a specific position in a doubly linked list
Java Code:
class Node {
int data;
Node prev;
Node next;
// Constructor to create a new node
Node(int d) {
data = d;
prev = null;
next = null;
}
}
class DoublyLinkedList {
Node head;
// Function to delete a node from specific position
public void deleteNodeFromPosition(int position) {
// If linked list is empty
if (head == null) {
return;
}
// If the position to delete is 0
// i.e. delete the head node
if (position == 0) {
head = head.next;
head.prev = null;
return;
}
// Get the node at the position to delete
Node current = head;
int count = 0;
while (count < position && current != null) {
current = current.next;
count++;
}
// If the position is greater than the size of the list
if (current == null || current.next == null) {
return;
}
// Unlink the node from the list
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
Python code:
class Node:
def __init__(self, data=None, prev=None, next=None):
self.data = data
self.prev = prev
self.next = next
class DoublyLinkedList:
def __init__(self):
self.head = None
def delete_node_from_position(self, position):
# If linked list is empty
if self.head is None:
return
# If the position to delete is 0
# i.e. delete the head node
if position == 0:
self.head = self.head.next
self.head.prev = None
return
# Get the node at the position to delete
current = self.head
count = 0
while count < position and current is not None:
current = current.next
count += 1
# If the position is greater than the size of the list
if current is None or current.next is None:
return
# Unlink the node from the list
current.prev.next = current.next
current.next.prev = current.prev
Implementation of deleting a node with specific value in a doubly linked list in Java:
class Node {
int data;
Node next, prev;
};
class DoublyLinkedList {
Node head;
int size;
public DoublyLinkedList() {
head = null;
size = 0;
}
public void add(int data) {
Node node = new Node();
node.data = data;
node.next = head;
node.prev = null;
if (head != null) {
head.prev = node;
}
head = node;
size++;
}
public void deleteValue(int value) {
Node node = head;
while (node != null && node.data != value) {
node = node.next;
}
if (node == null) {
System.out.println("Value not found");
} else {
if (node.prev != null) {
node.prev.next = node.next;
} else {
head = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}
size--;
}
}
}
Implementation of the same in Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.size = 0
def add(self, data):
node = Node(data)
node.next = self.head
node.prev = None
if self.head is not None:
self.head.prev = node
self.head = node
self.size += 1
def deleteValue(self, value):
node = self.head
while node is not None and node.data != value:
node = node.next
if node is None:
print("Value not found")
else:
if node.prev is not None:
node.prev.next = node.next
else:
self.head = node.next
if node.next is not None:
node.next.prev = node.prev
self.size -= 1
In both codes, the deleteValue
method starts at the head of the linked list and searches for the first node with the specified value. If the value is found, the links of the surrounding nodes are updated to exclude the node with the specified value. If the node with the specified value is not found, an error message is printed.