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 this:
It is important to note that this approach will only work if it is guaranteed that the given pointer does not point to the last node. Because if it is the last node, then you don’t have a next node to copy the data from.
struct Node *temp = node_ptr->next;
node_ptr->data = temp->data;
node_ptr->next = temp->next;
free(temp);
Below is the implementation of the above code:
C++
#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* head)
{
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " " ;
temp = temp->next;
}
}
void deleteNode(Node* node_ptr)
{
if (node_ptr->next == NULL)
{
free (node_ptr);
return ;
}
Node* temp = node_ptr->next;
node_ptr->data = temp->data;
node_ptr->next = temp->next;
free (temp);
}
int main()
{
Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
cout << "Before deleting \n" ;
printList(head);
deleteNode(head);
cout << "\nAfter deleting \n" ;
printList(head);
return 0;
}
|
C
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
}
void deleteNode( struct Node* node_ptr)
{
if (node_ptr->next == NULL)
{
free (node_ptr);
return ;
}
struct Node* temp = node_ptr->next;
node_ptr->data = temp->data;
node_ptr->next = temp->next;
free (temp);
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf ( "\n Before deleting \n" );
printList(head);
deleteNode(head);
printf ( "\n After deleting \n" );
printList(head);
getchar ();
}
|
Java
import java.util.*;
import java.io.*;
public class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
void deleteNode(Node node)
{
Node temp = node.next;
node.data = temp.data;
node.next = temp.next;
System.gc();
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 1 );
list.head.next = new Node( 12 );
list.head.next.next = new Node( 1 );
list.head.next.next.next = new Node( 4 );
list.head.next.next.next.next = new Node( 1 );
System.out.println( "Before Deleting " );
list.printList(head);
list.deleteNode(head);
System.out.println( "" );
System.out.println( "After deleting " );
list.printList(head);
}
}
|
Python3
class Node():
def __init__( self ):
self .data = None
self . next = None
def push(head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(head):
temp = head
while (temp ! = None ):
print (temp.data, end = ' ' )
temp = temp. next
def deleteNode(node_ptr):
temp = node_ptr. next
node_ptr.data = temp.data
node_ptr. next = temp. next
if __name__ = = '__main__' :
head = None
head = push(head, 1 )
head = push(head, 4 )
head = push(head, 1 )
head = push(head, 12 )
head = push(head, 1 )
print ( "Before deleting " )
printList(head)
deleteNode(head)
print ( "\nAfter deleting" )
printList(head)
|
C#
using System;
public class LinkedList {
Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
void printList(Node node)
{
while (node != null ) {
Console.Write(node.data + " " );
node = node.next;
}
}
void deleteNode(Node node)
{
Node temp = node.next;
node.data = temp.data;
node.next = temp.next;
}
public static void Main()
{
LinkedList list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(12);
list.head.next.next = new Node(1);
list.head.next.next.next = new Node(4);
list.head.next.next.next.next = new Node(1);
Console.WriteLine( "Before Deleting " );
list.printList(list.head);
list.deleteNode(list.head);
Console.WriteLine( "" );
Console.WriteLine( "After deleting " );
list.printList(list.head);
}
}
|
Javascript
<script>
var head;
class Node{
constructor(val){
this .data = val;
this .next = null ;
}
}
function printList(node){
while (node != null ){
document.write(node.data + " " );
node = node.next;
}
}
function deleteNode(node) {
var temp = node.next;
node.data = temp.data;
node.next = temp.next;
}
head = new Node(1);
head.next = new Node(12);
head.next.next = new Node(1);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(1);
document.write( "Before Deleting<br/> " );
printList(head);
deleteNode(head);
document.write( "<br/>" );
document.write( "After deleting<br/> " );
printList(head);
</script>
|
OutputBefore deleting
1 12 1 4 1
After deleting
12 1 4 1
Time complexity: O(1) since performing constant operations and modifying only single pointer to delete node
Auxiliary Space: O(1)
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.
Try this problem in a doubly-linked list.