Delete a Node in a Singly Linked List with Only a Pointer
Last Updated :
11 Dec, 2024
Given only a pointer to a node to be deleted in a singly linked list. The task is to delete that node from the list.
Note: The given pointer does not point to the last node of the linked list.
Examples:
Input: LinkedList: 1->2->3->4->5, delete_ptr = 2
Output: LinkedList: 1->3->4->5
Input: LinkedList: 1->2->3->4->5, delete_ptr = 3
Output: LinkedList: 1->2->4->5
Approach:
To remove the specified node, we first copy the value from the next node into the current node, then modify the next pointer to bypass the next node.
- Copy the value from the next node into the current node.
- Modify the next pointer to bypass the next node.
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
// Function to delete the node when only a pointer
// to that node is given
void deleteNode(Node *delete_ptr) {
// Store the pointer to the next node
Node *nextNode = delete_ptr->next;
// Copy the data of the next node to the current node
delete_ptr->data = delete_ptr->next->data;
// Point the current node to the next of next node
delete_ptr->next = delete_ptr->next->next;
// Delete the next node
delete nextNode;
}
// Function to print the linked list
void printList(Node *head) {
Node *curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
}
int main() {
// Create a hard-coded linked list: 1 -> 2 -> 3 -> 4 -> 5
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
// Node to be deleted
Node *delete_ptr = head->next;
deleteNode(delete_ptr);
printList(head);
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to delete the node when only a pointer
// to that node is given
void deleteNode(struct Node* delete_ptr) {
if (delete_ptr == NULL || delete_ptr->next == NULL) {
return;
}
// Store the pointer to the next node
struct Node* nextNode = delete_ptr->next;
// Copy the data of the next node to the current node
delete_ptr->data = nextNode->data;
// Point the current node to the next of next node
delete_ptr->next = nextNode->next;
// Delete the next node
free(nextNode);
}
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 data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
// Node to be deleted
struct Node* delete_ptr = head->next;
deleteNode(delete_ptr);
printList(head);
return 0;
}
Java
// Java program to delete the node
// in which only a single pointer
// is known pointing to that node
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to delete the node when only a pointer
// to that node is given
static void deleteNode(Node delete_ptr) {
// Copy the data of the next node to the current node
delete_ptr.data = delete_ptr.next.data;
// Point the current node to the next of next node
delete_ptr.next = delete_ptr.next.next;
}
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) {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
// Node to be deleted
Node delete_ptr = head.next;
deleteNode(delete_ptr);
printList(head);
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to delete the node when only a pointer
# to that node is given
def deleteNode(delete_ptr):
# Copy the data of the next node to the current node
delete_ptr.data = delete_ptr.next.data
# Point the current node to the next of next node
delete_ptr.next = delete_ptr.next.next
def printList(head):
curr = head
while curr:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hard-coded linked list:
# 1 -> 2 -> 3 -> 4 -> 5
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
# Node to be deleted
delete_ptr = head.next
deleteNode(delete_ptr)
printList(head)
C#
using System;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to delete the node when only a pointer
// to that node is given
static void deleteNode(Node delete_ptr) {
// Copy the data of the next node to the current node
delete_ptr.data = delete_ptr.next.data;
// Point the current node to the next of next node
delete_ptr.next = delete_ptr.next.next;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
// Node to be deleted
Node delete_ptr = head.next;
deleteNode(delete_ptr);
printList(head);
}
}
JavaScript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to delete the node when only a pointer
// to that node is given
function deleteNode(delete_ptr) {
// Copy the data of the next node to the current node
delete_ptr.data = delete_ptr.next.data;
// Point the current node to the next of next node
delete_ptr.next = delete_ptr.next.next;
}
function printList(head) {
let curr = head;
while (curr !== null) {
process.stdout.write(curr.data + " ");
curr = curr.next;
}
console.log();
}
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
// Node to be deleted
let delete_ptr = head.next;
deleteNode(delete_ptr);
printList(head);
Time complexity: O(1)
Auxiliary Space: O(1)