Topicsβ€ΊLinked Listβ€ΊDelete Node Without Head Reference
πŸ“–

Delete Node Without Head Reference

Linked List
~20 min read
5 practice problems

Overview

Delete node without head reference means you are given a pointer to the node to delete (and no access to the head). In a singly linked list you cannot go backward, so you cannot update the previous node's next pointer directly. The trick: copy the next node's value into the current node, then skip (delete) the next node by setting node.next = node.next.next. This effectively removes the "current" node from the list in O(1) time. The list now has the same values in the same order, but the node you were given has been "replaced" by the next node (which is no longer in the list).

Caveat: This does not work if the node to delete is the tail (it has no next). The problem usually states that the node is not the tail, or you can treat tail as a special case (e.g. you cannot delete the tail with this method without head).


When to Use

  • Delete when you only have the node β€” Interview trick; not always applicable (e.g. if external references to nodes must stay valid).
  • In-place "delete" by value replacement β€” When you can't access the head.

How It Works

  1. If node.next is null β€” You cannot delete the tail without the head; problem typically says node is not tail.
  2. Otherwise: node.val = node.next.val; node.next = node.next.next. The next node is effectively removed; the current node now holds the next node's value and points to what was after it.

Example 1: Delete Node (Not Tail)

Problem: Write a function to delete a node in a singly linked list. You will not be given access to the head; instead you will be given access to the node to be deleted directly. It is guaranteed that the node to be deleted is not a tail node.

function deleteNode(node: ListNode): void {
  node.val = node.next!.val;
  node.next = node.next!.next;
}
  • Time: O(1), Space: O(1).

Edge Cases

  • Node is tail β€” Cannot do it without head; problem usually excludes this.
  • Single node (and it's the one to delete) β€” Then it would be the tail; excluded.
  • Second-to-last node β€” Copy last node's value, set next to null; the last node is no longer reachable. Correct.

Common Mistakes

  1. Deleting the tail β€” If you have no head, you cannot make the previous node point to null; the problem must state the node is not the tail.
  2. Losing the list β€” Only update node.val and node.next; don't overwrite the node reference itself in a way that breaks the list.
  3. External references β€” If other code holds a reference to the "deleted" node, that reference still points to a node that is now in the list (with a different value). If they held a reference to the "next" node, that node is now unreachable. Clarify in interviews if this is acceptable.

Time and Space Complexity

  • Time: O(1).
  • Space: O(1).

Summary

  • Delete without head (singly linked, node not tail): Copy next's value into node; set node.next = node.next.next. The next node is effectively removed.
  • For Doubly Linked List you can delete any node (including tail) in O(1) with prev/next updates. See Dummy Head when you do have head and want to delete by value or position.