Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?

Given a pointer to a node to be deleted, delete the node. Note that we don’t have a pointer to the head node.

A simple solution is to traverse the linked list until you find the node you want to delete. But this solution requires a pointer to the head node, which contradicts the problem statement.
The fast solution is to copy the data from the next node to the node to be deleted and delete the next node. Something like the following.

// Find next node using next pointer
struct Node *temp = node_ptr->next;

// Copy data of next node to this node
node_ptr->data = temp->data;

node_ptr->next = temp->next;

// Delete next node
free(temp);

Program:

Java

 class LinkedList {     Node head; // head of the list       class Node {         int data;         Node next;         Node(int d)         {             data = d;             next = null;         }     }       /* Given a reference to the head of a list and an int,         inserts a new Node on the front of the list. */     public void push(int new_data)     {         /* 1. alloc the Node and put the data */         Node new_Node = new Node(new_data);           /* 2. Make next of new Node as head */         new_Node.next = head;           /* 3. Move the head to point to new Node */         head = new_Node;     }       /* This function prints contents of linked list         starting from the given Node */     public void printList()     {         Node tNode = head;         while (tNode != null) {             System.out.print(tNode.data + " ");             tNode = tNode.next;         }     }       public void deleteNode(Node Node_ptr)     {         Node temp = Node_ptr.next;         Node_ptr.data = temp.data;         Node_ptr.next = temp.next;         temp = null;     }       public static void main(String[] args)     {         LinkedList llist = new LinkedList();           /* Use push() to construct below list         1->12->1->4->1  */         llist.push(1);         llist.push(4);         llist.push(1);         llist.push(12);         llist.push(1);           System.out.println("Before deleting");         llist.printList();           /* I m deleting the head itself.         You can check for more cases */         llist.deleteNode(llist.head);           System.out.println("\nAfter Deleting");         llist.printList();     } } // This code is contributed by Rajat Mishra

C#

 using System;       public class LinkedList {     Node head; // head of the list       public class Node     {         public int data;         public Node next;         public Node(int d)         {             data = d;             next = null;         }     }       /* Given a reference to the head of a list and an int,         inserts a new Node on the front of the list. */     public void push(int new_data)     {         /* 1. alloc the Node and put the data */         Node new_Node = new Node(new_data);           /* 2. Make next of new Node as head */         new_Node.next = head;           /* 3. Move the head to point to new Node */         head = new_Node;     }       /* This function prints contents of linked list         starting from the given Node */     public void printList()     {         Node tNode = head;         while (tNode != null)         {             Console.Write(tNode.data + " ");             tNode = tNode.next;         }     }       public void deleteNode(Node Node_ptr)     {         Node temp = Node_ptr.next;         Node_ptr.data = temp.data;         Node_ptr.next = temp.next;         temp = null;     }       // Driver code     public static void Main(String[] args)     {         LinkedList llist = new LinkedList();           /* Use push() to construct below list         1->12->1->4->1 */         llist.push(1);         llist.push(4);         llist.push(1);         llist.push(12);         llist.push(1);           Console.WriteLine("Before deleting");         llist.printList();           /* I m deleting the head itself.         You can check for more cases */         llist.deleteNode(llist.head);           Console.WriteLine("\nAfter Deleting");         llist.printList();     } }   // This code is contributed by 29AjayKumar

Javascript



Output:

Before deleting
1 12 1 4 1
After deleting
12 1 4 1

Time Complexity:

• For printing linked list: O(N)
• For inserting node: O(1)
• For deleting node: O(N)
• Auxiliary Space: O(1)

This solution doesn’t work if the node to be deleted is the last node of the list. To make this solution work, we can mark the end node as a dummy node. But the programs/functions that are using this function should also be modified.
Exercise: Try this problem with the doubly linked list.

One line in the function deletenode():

C++

 void deleteNode(Node *node) {    *node = *(node->next); }

Java

 static void deleteNode(Node node) {    node = (node.next); }   // This code is contributed by umadevi9616

Python3

 def deleteNode(Node Node):    Node = (Node.next);   # This code is contributed by gauravrajput1

C#

 void deleteNode(Node *node) {    *node = *(node->next); }   // This code is contributed by shubhamsingh10

Javascript

 function deleteNode(node) {    node = (node.next); }   // This code is contributed by gauravrajput1