# Delete a node from linked list without head pointer in Java

## Delete a Node from linked list without head pointer

You are given a singly linked list and pointer which is pointing to the node which is required to be deleted. Any information about head pointer or any other node is not given. You need to write a function to delete that node from linked list. Your function will take only one argument: pointer to the node which is to be deleted.

Note: No head reference is given to you.
It is guaranteed that the node to be deleted is not the last node;

Examples:
A linked list is built as: Defination of each node is as follows: struct Node { int data; struct Node* next; }; Input : C (a pointer to C) Output : A–>B–>D–>E–>F Input : A (a pointer to A) Output : B–>D–>E–>F

It would be a simple deletion problem from the singly linked list if the head pointer was given because for deletion you must know the previous node and you can easily reach there by traversing from the head pointer. Conventional deletion is impossible without knowledge of the previous node of a node that needs to be deleted.
The trick here is we can copy the data of the next node to the data field of the current node to be deleted. Then we can move one step forward. Now our next has become the current node and the current has become the previous node. Now we can easily delete the current node by conventional deletion methods.
For example, suppose we need to delete C and a pointer to C is given. If we had a pointer to B we could have deleted C easily. But here we will copy the data field of D to the data field of C. Then we will move forward. Now we are at D and we have a pointer to C i.e. the previous pointer. So we will delete D. That’s how node C will be deleted.
Below image is a dry run of the above approach: Below is the implementation of the above approach :

CPP

// C++ program to delete a node

#include <bits/stdc++.h>

using namespace std;

/* Link list node */

struct Node {

int data;

struct Node* next;

};

// Function to delete the node without head

void deleteNodeWithoutHead(struct Node* pos)

{

if (pos == NULL) // If linked list is empty

return;

else {

if (pos->next == NULL) {

printf(“This is last node, require head, can’t ”

“be freed\n”);

return;

}

struct Node* temp = pos->next;

// Copy data of the next node to current node

pos->data = pos->next->data;

// Perform conventional deletion

pos->next = pos->next->next;

free(temp);

}

}

// Function to print the linked list

{

Node* temp = head;

while (temp) {

cout << temp->data << ” -> “;

temp = temp->next;

}

cout << “NULL”;

}

void push(struct Node** head_ref, int new_data)

{

/* allocate node */

struct Node* new_node = new Node();

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

/* move the head to point to the new node */

}

// Driver Code

int main()

{

/* Start with the empty list */

struct Node* head = NULL;

// create linked 35->15->4->20

cout << “Initial Linked List: \n”;

cout << endl << endl;

// Delete 15 without sending head

Node* del = head->next;

// Print the final linked list

cout << “Final Linked List after deletion of 15:\n”;

return 0;

// This code has been contributed by Striver

}

Output:
Initial Linked List: 35 -> 15 -> 4 -> 20 -> NULL Final Linked List after deletion of 15: 35 -> 4 -> 20 -> NULL

This article is contributed by Jeet Jain. Please see Given only a pointer to a node to be deleted in a singly linked list, how do you delete it? for more details and complete implementation. Article Tags :

Technical Scripter

MAQ Software

programming-puzzle

Technical Scripter 2018

Practice Tags :

MAQ Software

Baca Juga :   Suatu alat yang dapat mengubah getaran listrik menjadi gerak dinamakan

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

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++

// C++ program to del the node

// in which only a single pointer

// is known pointing to that node

#include <assert.h>

#include <bits/stdc++.h>

using namespace std;

/* Link list node */

class Node {

public:

int data;

Node* next;

};

/* Given a reference (pointer to pointer) to the head

of a list and an int, push a new node on the front

of the list. */

void push(Node** head_ref, int new_data)

{

/* allocate node */

Node* new_node = new Node();

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

/* move the head to point to the new node */

}

{

Node* temp = head;

while (temp != NULL) {

cout << temp->data << ” “;

temp = temp->next;

}

}

void deleteNode(Node* node_ptr)

{

// If the node to be deleted is the

// last node of linked list

if (node_ptr->next == NULL)

{

free(node_ptr);

// this will simply make the node_ptr NULL.

return;

}

// if node to be deleted is the first or

// any node in between the linked list.

Node* temp = node_ptr->next;

node_ptr->data = temp->data;

node_ptr->next = temp->next;

free(temp);

}

// Driver code

int main()

{

/* Start with the empty list */

Node* head = NULL;

/* Use push() to construct below list

1->12->1->4->1 */

cout << “Before deleting \n”;

/* I m deleting the head itself.

You can check for more cases */

cout << “\nAfter deleting \n”;

return 0;

}

// This code is contributed by rathbhupendra

C

// C++ program to del the node

// in which only a single pointer

// is known pointing to that node

#include <assert.h>

#include <stdio.h>

#include <stdlib.h>

/* Link list node */

struct Node {

int data;

struct Node* next;

};

/* Given a reference (pointer to pointer) to the head

of a list and an int, push a new node on the front

of the list. */

void push(struct Node** head_ref, int new_data)

{

/* allocate node */

struct Node* new_node

= (struct Node*)malloc(sizeof(struct Node));

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

/* move the head to point to the 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 the node to be deleted is the last

// node of linked list

if (node_ptr->next == NULL)

{

free(node_ptr);

// this will simply make the node_ptr NULL.

return;

}

struct Node* temp = node_ptr->next;

node_ptr->data = temp->data;

node_ptr->next = temp->next;

free(temp);

}

// Driver code

int main()

{

/* Start with the empty list */

struct Node* head = NULL;

/* Use push() to construct below list

1->12->1->4->1 */

printf(“\n Before deleting \n”);

/* I m deleting the head itself.

You can check for more cases */

printf(“\n After deleting \n”);

getchar();

}

Java

// Java program to del the node in

// which only a single pointer is

// known pointing to that node

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();

}

// Driver code

public static void main(String[] args)

{

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 “);

/* I m deleting the head itself.

You can check for more cases */

System.out.println(“”);

System.out.println(“After deleting “);

}

}

// This code has been contributed by Mayank Jaiswal

Python3

# A python3 program to delete

# the node in which only a single pointer

# is known pointing to that node

# Linked list node

class Node():

def __init__(self):

self.data = None

self.next = None

# Given a reference (pointer to pointer)

# to the head of a list and an int,

# push a new node on the front of the list

# allocate node

new_node = Node()

# put in the data

new_node.data = new_data

# link the old list off the new node

# move the head to point to the new node

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

# Driver code

if __name__ == ‘__main__’:

# Use push() to construct below list

# 1->12->1->4->1

print(“Before deleting “)

# I’m deleting the head itself.

# You can check for more cases

print(“\nAfter deleting”)

# This code is contributed by Yashyasvi Agarwal

C#

// C# program to del the node in

// which only a single pointer is

// known pointing to that node

using System;

public class LinkedList {

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;

}

// Driver code

public static void Main()

{

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 “);

/* I m deleting the head itself.

You can check for more cases */

Console.WriteLine(“”);

Console.WriteLine(“After deleting “);

}

}

/* This code contributed by PrinciRaj1992 */

Javascript

<script>

// javascript program to del the node in

// which only a single pointer is

// known pointing to that node

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;

}

// Driver code

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/> “);

/*

* I m deleting the head itself. You can check for more cases

*/

document.write(“<br/>”);

document.write(“After deleting<br/> “);

// This code is contributed by todaysgaurav

</script>

Output

Before deleting 1 12 1 4 1 After deleting 12 1 4 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. Article Tags :

Practice Tags :

## Delete a Node from linked list without head pointer in C++

In this tutorial, we are going to learn how to delete a node without head pointer in a singly linked list.

Let’s see the steps to solve the problem.

• Write struct with data, and next pointer.

• Write a function to insert the node into the singly linked list.

• Initialize the singly linked list with dummy data.

• Take a node from the linked list using the next pointer.

• Move the delete node to the next node.

## Delete a node from linked list without head pointer in python

Python program for Delete a node from linked list without head pointer. Here problem description and other solutions.