Delete without head pointer
GeeksForGeeks Problem Of The Day 16–03–24
This is my attempt to make the coding experience easier for you guys so that you can easily learn what to do in today’s problem of the day.
Always here to assist you guys.
Problem
You are given a node del_node of a Singly Linked List where you have to delete a value of the given node from the linked list but you are not given the head of the list.
By deleting the node value, we do not mean removing it from memory. We mean:
- The value of the given node should not exist in the linked list.
- The number of nodes in the linked list should decrease by one.
- All the values before & after the del_node node should be in the same order.
Note:
- Multiple nodes can have the same values as the del_node, but you must only remove the node whose pointer del_node is given to you.
- It is guaranteed that there exists a node with a value equal to the del_node value and it will not be the last node of the linked list.
Example 1:
Input:
Linked List = 1 -> 2
del_node = 1
Output:
2
Explanation:
After deleting 1 from the linked list,
we have remaining nodes as 2.Example 2:
Input:
Linked List = 10 -> 20 -> 4 -> 30
del_node = 20
Output:
10 4 30
Explanation:
After deleting 20 from the linked list,
we have remaining nodes as 10, 4, 30.Your Task:
You don’t need to read or print anything. You only need to complete the function deleteNode() which takes a reference of the deleting node value & your task is to delete the given node value.
Expected Time Complexity: O(1).
Expected Auxilliary Space: O(1).
Get Abhishek Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Constraints:
2 <= n <= 10³
1 <= elements of the linked list <= 10⁹
Intuition
The goal is to delete a node from a singly linked list given only access to that node (del_node). Deleting a node usually requires access to the previous node as well, but in this case, we only have access to the node to be deleted.
Approach
Special Cases Handling :
- First, I checked if the given node to be deleted (
del_node) is null. If it is null, it implied that the linked list is empty, so there's nothing to delete. - Next, I checked if the next node of
del_nodeis null. If it is null, it meansdel_nodeis the last node of the linked list. In this case, I cannot directly deletedel_node, so I returned without performing any deletion.
Copied Data and Updated Pointers :
- If neither of the above special cases holds true, it meant
del_nodeis neither null nor the last node of the linked list. - To delete
del_node, I copied the data from its next node todel_node. This effectively makeddel_nodecontain the data of its next node. - Next, I updated the
nextpointer ofdel_nodeto skip the next node and point directly to the node after the next node. This effectively removed the next node from the linked list.
My approach allows to delete a node from a singly linked list given only access to that node, without needing access to the previous node.
Have a look at the code , still have any confusion then please let me know in the comments … Keep Solving.:)
Complexity
- Time Complexity : O( 1 )
- Space Complexity : O( 1 )
Code
// User function Template for Java
/*
class Node
{
int data ;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
*/
//Function to delete a node without any reference to head pointer.
class Solution
{
void deleteNode(Node del_node)
{
// Your code here
// Checking if the linked list is empty
if(del_node == null)
return;
// Checking if del_node is the last node
if(del_node.next == null)
return;
// Copying data from the next node to del_node
del_node.data = del_node.next.data;
// Updating pointers to delete the next node
del_node.next = del_node.next.next;
}
}


