Linked lists are one of the most fundamental data structures in programming, and deleting a node from a linked list is a common operation. However, it usually requires the head pointer to traverse and manipulate the list; in some cases, we may need to delete a node without access to the head pointer. Let’s look at how we solve this problem.
Given a single linked list, we have to delete a specific node. The node only contains two data points: its value and a pointer to the next node. Additionally, we don’t have access to the head pointer, just the pointer pointing to the node that is to be deleted. This scenario can occur in different tasks, such as when working with certain modified data structures or with restricted access to the linked list.
This approach involves copying the data from each node that comes after the node that is to be deleted. This leaves the last node as a duplicate, which we can then be deleted. This approach is highly inefficient because we traverse the entire list that exists after the node is deleted, giving us a time complexity of
next
data and repointing The most efficient approach to deleting a node without the head pointer involves changing the current node’s value to the value of the next
node and updating the next
pointer of the current node to its next
's next
node. This method has a time complexity of
Here are the steps we must take to delete the node in the most efficient way:
Checking for validity: Before performing any operation, we must ensure that the node we want to delete (del_node
) is not null or the last node in the linked list. If it is, we won’t be able to delete it, but we can return or handle the situation accordingly. Without the head pointer, we can’t delete the last node of the list because we do not have access to the node before it.
Copying data from the next node: The first step in the algorithm is to copy the data from the next node into del_node
. We can do this by setting del_node.data
to del_node.next.data
.
Updating the next
pointer: After copying the data, the next step is to update the next
pointer of del_node
. We must ensure that it now points to the node that comes after the next node. This effectively skips the next node in the linked list as if it no longer exists. To do this, we set del_node.next
to del_node.next.next
.
Delete the next node: The final step is to delete the next node (the node that originally came after del_node
). This can be done in various ways depending on your language or data structure. In Python, for instance, the garbage collector would automatically remove the next node if there are no more references to it.
Now, let's look at how this algorithm looks like in Python.
def delete_node(del_node):if del_node is None or del_node.next is None:return False # Handles if node is invalid or last node.del_node.data = del_node.next.datadel_node.next = del_node.next.next
The function first checks if del_node
is None
or if its next node is None
, and returns the function if that is the case. This ensures that the function handles invalid nodes. Next, we simply copy the data of the next
node to the current node, and set the next
of our current node to the next
of the next
node. We can see the function in action below:
# Defining the Linked-List node.class ListNode:def __init__(self, data):self.data = dataself.next = None# Deleting function.def delete_node(del_node):if del_node is None or del_node.next is None:return False # Handles if node is invalid or last node.del_node.data = del_node.next.datadel_node.next = del_node.next.next# Printing function (it uses the head pointer but is only here to visualize the answer).def print_linked_list(head):current = headwhile current:print(current.data, end=" -> ")current = current.nextprint("None")# Create a sample linked list: 1 -> 2 -> 3 -> 4node1 = ListNode(1)node2 = ListNode(2)node3 = ListNode(3)node4 = ListNode(4)node1.next = node2node2.next = node3node3.next = node4print("Original List: ")print_linked_list(node1)# Deleting the third node and printing the linked list.delete_node(node3)print("\nList after deletion of third node: ")print_linked_list(node1)
Deleting a node from a linked list without the head pointer may seem challenging, but with the efficient approach described in this Answer, we can achieve this task with a time complexity of
Free Resources