Delete without head pointer

Last Updated : 27 Jul, 2026

Given a node of a singly linked list. Delete this node from the list without access to the head pointer.

After deletion:

  • The number of nodes in the linked list should decrease by one.
  • The relative order of the remaining nodes should remain unchanged.

Note: It is guaranteed that x is not the last node of the linked list.

Examples:

Input: head = 1 -> 2, x = 1

3

Output: 2
Explanation: After deleting 1 from the linked list, we have remaining nodes as 2.

4


Input: head = 10 -> 20 -> 4 -> 30, x = 20

5

Output: 10->4->30
Explanation: After deleting 20 from the linked list, we have remaining nodes as 10, 4, 30.

6
Try It Yourself
redirect icon

Copy Data of Next Node - O(1) Time and O(1) Space

The idea is to copy the data of the next node into the given node and then delete the next node. This removes the given value from the linked list without needing access to the head pointer. Since the given node is never the last node, this operation is always possible.

Working of Approach:

  • Store the next node of the given node.
  • Copy the next node's data into the given node.
  • Update the given node's next pointer to skip the next node.
  • Delete the skipped node.
  • The linked list size decreases by one.

Let us understand with an example:
Input: head = 10 -> 20 -> 4 -> 30, x = 20

5
  • Store the next node (4) in a temporary pointer.
  • Copy the data of the next node (4) into x. The list becomes: 10 -> 4 -> 4 -> 30.
  • Update the next pointer of x to skip the copied node. The list becomes: 10 -> 4 -> 30.
  • Delete the skipped node (4), reducing the size of the linked list by one.
  • The final linked list is: 10 -> 4 -> 30.
6
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

// Function to delete the given node
void deleteNode(Node *x)
{

    // Store the next node
    Node *temp = x->next;

    // Copy next node's data
    x->data = temp->data;

    // Skip the next node
    x->next = temp->next;

    // Delete the skipped node
    delete temp;
}

// Function to print the linked list
void printList(Node *head)
{
    while (head != nullptr)
    {
        cout << head->data;
        if (head->next)
            cout << " -> ";
        head = head->next;
    }
    cout << endl;
}

int main()
{

    Node *head = new Node(10);
    head->next = new Node(20);
    head->next->next = new Node(4);
    head->next->next->next = new Node(30);

    Node *x = head->next;

    deleteNode(x);

    printList(head);

    return 0;
}
Java
class Node {
    int data;
    Node next;

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

class GFG {

    // Function to delete the given node
    static void deleteNode(Node x)
    {

        // Store the next node
        Node temp = x.next;

        // Copy next node's data
        x.data = temp.data;

        // Skip the next node
        x.next = temp.next;
    }

    // Function to print the linked list
    static void printList(Node head)
    {
        while (head != null) {
            System.out.print(head.data);
            if (head.next != null)
                System.out.print(" -> ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {

        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(4);
        head.next.next.next = new Node(30);

        Node x = head.next;

        deleteNode(x);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None

# Function to delete the given node


def deleteNode(x):

    # Store the next node
    temp = x.next

    # Copy next node's data
    x.data = temp.data

    # Skip the next node
    x.next = temp.next

    # Delete the skipped node
    del temp

# Function to print the linked list
def printList(head):
    while head is not None:
        print(head.data, end="")
        if head.next is not None:
            print(" -> ", end="")
        head = head.next
    print()


if __name__ == '__main__':
    head = Node(10)
    head.next = Node(20)
    head.next.next = Node(4)
    head.next.next.next = Node(30)

    x = head.next

    deleteNode(x)

    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 given node
    static void deleteNode(Node x)
    {
        // Store the next node
        Node temp = x.next;

        // Copy next node's data
        x.data = temp.data;

        // Skip the next node
        x.next = temp.next;
    }

    // Function to print the linked list
    static void PrintList(Node head)
    {
        while (head != null) {
            Console.Write(head.data);
            if (head.next != null)
                Console.Write(" -> ");
            head = head.next;
        }
        Console.WriteLine();
    }

    static void Main()
    {
        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(4);
        head.next.next.next = new Node(30);

        Node x = head.next;

        deleteNode(x);

        PrintList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

// Function to delete the given node
function deleteNode(x)
{

    // Store the next node
    const temp = x.next;

    // Copy next node's data
    x.data = temp.data;

    // Skip the next node
    x.next = temp.next;

    // Delete the skipped node
    temp.next = null;
}

// Function to print the linked list
function printList(head)
{
    let current = head;
    while (current !== null) {
        process.stdout.write(current.data.toString());
        if (current.next !== null) {
            process.stdout.write(" -> ");
        }
        current = current.next;
    }
    console.log();
}

// Driver Code
const head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(4);
head.next.next.next = new Node(30);

const x = head.next;

deleteNode(x);

printList(head);

Output
10 -> 4 -> 30

Note: This technique works only when the given node is not the last node. If the last node needs to be deleted, we must have access to either the previous node or the head pointer to update the previous node's next pointer. Since this problem guarantees that x is not the last node, the above approach always works.

Comment