Delete without head pointer | Practice | GeeksforGeeks

You are given a pointer/ reference to the node which is to be deleted from the linked list of N nodes. The task is to delete the node. Pointer/ reference to head node is not given. 
Note: No head reference is given to you. It is guaranteed that the node to be deleted is not a tail node in the linked list.
Example 1:
Input:
N = 2
value[] = {1,2}
node = 1
Output: 2
Explanation: After deleting 1 from the
linked list, we have remaining nodes
as 2.

Example 2:
Input:
N = 4
value[] = {10,20,4,30}
node = 20
Output: 10 4 30
Explanation: After deleting 20 from
the linked list, we have remaining
nodes as 10, 4 and 30.
Your Task:
You only need to complete the function deleteNode that takes reference to the node that needs to be deleted. The printing is done automatically by the driver code.
Expected Time Complexity : O(1)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 103
0
mannukhurana103971 day ago
Java
As we can't delete the current node, copy the data of next node to the current node and point the current node node to the next of the next node;
 
void deleteNode(Node del)
    { del.data = del.next.data;
      del.next = del.next.next;
    }
0
aakashsoni24861 week ago
just change the given pointer to next pointer.
// 1 line code class Solution{   public:   //Function to delete a node without any reference to head pointer.   void deleteNode(Node *del)   {      // Your code here      *del=*del->next;   }
};
+1
moonviprant2 weeks ago
Simple SOlution:->
class Solution
{
    public:
    //Function to delete a node without any reference to head pointer.
    void deleteNode(Node *del)
    {
       // Your code here
       del->data=del->next->data;
       del->next=del->next->next;
    }

};
+1
tarungarg0513 weeks ago
void deleteNode(Node del)   {        // Your code here                del.data = del.next.data;        del.next = del.next.next;   }
0
adityaraj100mit4 weeks ago
void deleteNode(Node *del)   {      // Your code here      Node* temp=del->next;      del->data=temp->data;      del->next=temp->next;      free(temp);         }
+1
swatisingh727774 weeks ago
  void deleteNode(Node *del)
    {
       
       Node* temp = del->next;
       del->data = del->next->data;
       del->next = del->next->next;
       temp->next=NULL;
    }
    // O(1) time complexity
+1
umidbaban121 month ago
void deleteNode(Node *del)   {      del->data=del->next->data;      del->next=del->next->next;   }
0
amishasahu3281 month ago
 void deleteNode(Node *del)
    {
       // Your code here
      Node *temp = del->next;
      del->data = temp->data;
      del->next = temp->next;
    }
0
ahmadsqf1 month ago
What if we only do this ?

void deleteNode(Node *del)
{
    del = del -> next;
}
+1
abhishekpanwar6971 month ago
void deleteNode(Node *start)   {      swap(start->data,start->next->data);      start->next=start->next->next;         }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.