Skip to content
Veltoryx Technologies
FreeProblem 43● MediumLeetCode #237 ↗5 min readUpdated 30 October 2026

Delete Node in a Linked List

Delete a node from a singly linked list in O(1) time when only given direct access to the node itself and not the head.

01The problem

There is a singly-linked list head and we want to delete a node in it.

You are given the node to be deleted node. You will not be given access to the first node of head.

All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.

Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:

  • The value of the given node should not exist in the linked list.
  • The number of nodes in the linked list should decrease by one.
  • All the values before node should be in the same order.
  • All the values after node should be in the same order.
InputOutputWhy
[4, 5, 1, 9], node = 5[4, 1, 9]Value 5 is removed; list becomes 4 -> 1 -> 9
[4, 5, 1, 9], node = 1[4, 5, 9]Value 1 is removed; list becomes 4 -> 5 -> 9
[1, 2], node = 1[2]First node removed; only 2 remains

Constraints: 2 to 1,000 nodes, unique values, given node is guaranteed not to be the tail.

02How to spot it

  • The problem specifies that you are not given the head, only the target node to delete.
  • In a singly linked list, normal deletion requires updating prev.next = node.next, which is impossible without the predecessor.
  • Key observation: You cannot rewire the incoming pointer to your node, but you can overwrite what your node contains.
  • Copy the value of node.next into node, then bypass and unlink node.next.

So: value copy and link bypass trick.

03Pseudocode

text
node.val = node.next.val
node.next = node.next.next
  1. Look at the next node node.next.
  2. Copy its value into current node: node.val = node.next.val.
  3. Unlink the next node: node.next = node.next.next.

04Watch it work

Visualizer · step through the algorithmlist = [4,5,1,9], deleteVal = 5

linked list

40
51
12
93
node

Given reference to node with value 5 at index 1. No access to head or predecessor.

Pseudocode

  1. node.val = node.next.val
  2. node.next = node.next.next

Variables

targetNode
5
step 1 / 3

05Step-by-step dry run

  1. 1

    linked list

    40
    51
    12
    93
    node

    Given reference to node with value 5 at index 1. No access to head or predecessor.

    targetNode = 5

  2. 2

    linked list (value copied)

    40
    11
    12
    93
    nodenext

    Copy next node's value into current node: node.val = 1. Current node now holds 1.

    node.val = 1

  3. 3

    linked list (node unlinked)

    40
    11
    92
    node

    Bypass next node: node.next = node.next.next. The duplicate node is bypassed and unlinked.

    result = 4,1,9

06Java solution

DeleteNodeInLinkedList.java
public class DeleteNodeInLinkedList {
    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

    public static void deleteNode(ListNode node) {
        // Copy the next node's value into the current node
        node.val = node.next.val;
        // Bypass and unlink the next node
        node.next = node.next.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(4);
        head.next = new ListNode(5);
        head.next.next = new ListNode(1);
        head.next.next.next = new ListNode(9);

        // Delete the node containing value 5
        deleteNode(head.next);

        ListNode curr = head;
        while (curr != null) {
            System.out.print(curr.val + (curr.next != null ? " " : ""));
            curr = curr.next;
        }
        System.out.println();
    }
}
Expected output
4 1 9

Tricky lines, decoded

  • node.val = node.next.val: The node with value 5 adopts the value 1. The list temporarily reads 4 -> 1 -> 1 -> 9.
  • node.next = node.next.next: Unlinks the second 1, resulting in the clean sequence 4 -> 1 -> 9.

07Why it works

In an object-oriented memory model, an external consumer only observes a linked list as a sequence of accessible values. Because we cannot alter the pointer pointing to node, we turn node into an exact clone of its successor, then delete the duplicate successor. The effect on all traversals is indistinguishable from true node deletion.

Invariant: After execution, the list retains all elements in original order except for the original node.val.

08Time and space

  • Time: O(1). Exactly two reference and value assignments with no list traversal.
  • Space: O(1). Modifies the structure in place without extra memory.

09Edge cases

InputOutputWhy it matters
[4, 5, 1, 9], node = 5[4, 1, 9]Middle node deletion
[1, 2], node = 1[2]Deleting the initial node without head
[0, 1], node = 0[1]Node with value zero
[-3, 5, -99], node = -3[5, -99]Negative values
[1, 2, 3, 4], node = 3[1, 2, 4]Second-to-last node

10Common mistakes

  • Writing node = null. This only reassigns the local parameter reference. The original node in the heap remains connected to the list.
  • Attempting to traverse backwards. Singly linked lists have no backward pointers; without head, reaching the predecessor is impossible.
  • Applying this technique to the tail node. If node.next == null, node.next.val throws a NullPointerException. The problem guarantees node is never the tail.

11Other approaches

ApproachTimeSpaceUse when
Standard head deletionO(n)O(1)When head reference is available
Copy + skipO(1)O(1)Default solution when head is missing
Shift all values leftO(n)O(1)Unnecessary; copy once is sufficient

12In the interview

  • What if the target node were the tail? Without access to head or double-linking, deleting a tail node in O(1) is mathematically impossible because the preceding node must have its next pointer cleared to null.

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. Why does node = null fail to delete the node from the linked list?

  2. 2. Why cannot this algorithm delete the tail node of a singly linked list?

14Solve it yourself

Try implementing standard deletion when head is provided:

  1. Walk a curr pointer from head while checking curr.next.val == target.
  2. Handle the edge case where head.val == target using a dummy node.