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.
Examples:
Input: list = 10 -> 20 -> 4 -> 30, delNode = 20
Output: 10 -> 4 -> 30
Explanation: Node with value 20 is deleted.Input: list = 1 -> 2, delNode = 1
Output: 2
Explanation: Node with value 1 is deleted.
Approach:
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 idea is to copy the data from the next node into the given node and then deleting the next node.
Below is the implementation of the above approach:
// C++ code to delete node value
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
// Function to delete a node value when
// only a pointer to that node is given
void deleteNode(Node* delNode ) {
// Ensure the node to be deleted is not the last node
if (delNode == nullptr || delNode ->next == nullptr) {
return;
}
// Copy data from the next node into the current node
Node* temp = delNode ->next;
delNode ->data = temp->data;
// Link current node to the node after the next node
delNode ->next = temp->next;
// Delete the next node (the one whose data was copied)
delete temp;
}
void printList(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Creating a linked list: 4 -> 5 -> 6 -> 7 -> 8
Node* head = new Node(4);
head->next = new Node(5);
head->next->next = new Node(6);
head->next->next->next = new Node(7);
head->next->next->next->next = new Node(8);
deleteNode(head);
printList(head);
return 0;
}
// C code to delete node value
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to delete a node value when
// only a pointer to that node is given
void deleteNode(struct Node* delNode ) {
// Ensure the node to be deleted is not the last node
if (delNode == NULL || delNode ->next == NULL) {
return;
}
// Copy data from the next node into the current node
struct Node* temp = delNode ->next;
delNode ->data = temp->data;
// Link current node to the node after the next node
delNode ->next = temp->next;
// Delete the next node (the one whose data was copied)
free(temp);
}
void printList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int x) {
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = x;
new_node->next = NULL;
return new_node;
}
int main() {
// Creating a linked list: 4 -> 5 -> 6 -> 7 -> 8
struct Node* head = createNode(4);
head->next = createNode(5);
head->next->next = createNode(6);
head->next->next->next = createNode(7);
head->next->next->next->next = createNode(8);
deleteNode(head);
printList(head);
return 0;
}
// Java code to delete node value
class Node {
int data;
Node next;
Node(int val) {
data = val;
next = null;
}
}
class GfG {
// Function to delete a node value when
// only a pointer to that node is given
static void deleteNode(Node delNode) {
// Ensure the node to be deleted is not the last node
if (delNode == null || delNode.next == null) {
return;
}
// Copy data from the next node into the current node
Node temp = delNode.next;
delNode.data = temp.data;
// Link current node to the node after the next node
delNode.next = temp.next;
// Delete the next node (the one whose data was copied)
temp.next = null;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Creating a linked list: 4 -> 5 -> 6 -> 7 -> 8
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(6);
head.next.next.next = new Node(7);
head.next.next.next.next = new Node(8);
deleteNode(head);
printList(head);
}
}
# Python code to delete node value
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to delete a node value when
# only a pointer to that node is given
def deleteNode(delNode):
# Ensure the node to be deleted is not the last node
if delNode is None or delNode.next is None:
return
# Copy data from the next node into the current node
temp = delNode.next
delNode.data = temp.data
# Link current node to the node after the next node
delNode.next = temp.next
# Delete the next node (the one whose data was copied)
del temp
def printList(head):
curr = head
while curr:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Creating a linked list: 4 -> 5 -> 6 -> 7 -> 8
head = Node(4)
head.next = Node(5)
head.next.next = Node(6)
head.next.next.next = Node(7)
head.next.next.next.next = Node(8)
deleteNode(head)
printList(head)
// C# code to delete node value
using System;
class Node {
public int data;
public Node next;
public Node(int val) {
data = val;
next = null;
}
}
class GfG {
// Function to delete a node value when
// only a pointer to that node is given
static void deleteNode(Node delNode) {
// Ensure the node to be deleted is not the last node
if (delNode == null || delNode.next == null) {
return;
}
// Copy data from the next node into the current node
Node temp = delNode.next;
delNode.data = temp.data;
// Link current node to the node after the next node
delNode.next = temp.next;
// Delete the next node (the one whose data was copied)
temp.next = null;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Creating a linked list: 4 -> 5 -> 6 -> 7 -> 8
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(6);
head.next.next.next = new Node(7);
head.next.next.next.next = new Node(8);
deleteNode(head);
printList(head);
}
}
// Javascript code to delete node value
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to delete a node value when
// only a pointer to that node is given
function deleteNode(delNode) {
// Ensure the node to be deleted is not the last node
if (delNode === null || delNode.next === null) {
return;
}
// Copy data from the next node into the current node
const temp = delNode.next;
delNode.data = temp.data;
// Link current node to the node after the next node
delNode.next = temp.next;
// Delete the next node (the one whose data was copied)
temp.next = null;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Creating a linked list: 4 -> 5 -> 6 -> 7 -> 8
let head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(6);
head.next.next.next = new Node(7);
head.next.next.next.next = new Node(8);
deleteNode(head);
printList(head);
Output
5 6 7 8
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: The described method of copying data from the next node works well for all nodes except the last one. To delete the last node, we need either:
- A pointer/reference to the previous node,
- A pointer/reference to the head node to traverse and find the previous node.