1. Introduction

In traditional scenarios of deleting a node from a linked list, we have access to the head node, and we traverse the list to find the node and delete it. However, what if you are given a pointer to a node in a singly linked list and asked to delete that specific node without being given the head of the list? This challenge can be tackled using a unique approach. This blog post will demonstrate how to achieve this in Java.

2. Program Steps

1. Check if the given node is null, and if so, return without doing anything.

2. Check if the next node to the given node is null (i.e., the given node is the last node). If so, we can’t delete the node. Print an error message.

3. Copy the data from the next node to the given node.

4. Delete the next node.

3. Code Program

class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }
}

public class LinkedList {

    // This function deletes the node pointed to by 'node'.
    public static void deleteNodeWithoutHead(Node node) {
        if (node == null) {
            return;
        }

        if (node.next == null) {
            System.out.println("Can't delete the last node using this method");
            return;
        }

        node.data = node.next.data;
        node.next = node.next.next;
    }

    // Utility function to print the linked list
    public static void printList(Node head) {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);

        System.out.println("Before deleting the node:");
        printList(head);

        Node nodeToDelete = head.next;  // Node with value 2
        deleteNodeWithoutHead(nodeToDelete);

        System.out.println("After deleting the node:");
        printList(head);
    }
}

Output:

Before deleting the node:
1 2 3 4
After deleting the node:
1 3 4

4. Step By Step Explanation

1. The Node class defines the structure of each node in the linked list. Each node contains an integer data and a reference to the next node.

2. The deleteNodeWithoutHead function is tasked with deleting a given node without having access to the head of the linked list.

– First, we check if the given node is null. If so, we return without doing anything.

– Next, if the next node is null, it means we are trying to delete the last node of the list, which can't be achieved using this method.

– If the above conditions are not met, we copy the data from the next node to the current node and bypass the next node by adjusting the current node's next pointer.

3. The printList function is a utility to print the entire linked list starting from a given head node.

4. In the main function, we create a sample linked list and then delete a node from it using the deleteNodeWithoutHead function. The result before and after the deletion is printed to demonstrate the effect of the deletion.