Interview Question Series | Season 3

📝 Delete Node in a Linked List – Reading Version

Problem in short:
You have a singly-linked list and a node to delete, but you don’t have access to the head.
All values are unique, and the node to delete is not the last node.

Idea of the solution:
Since you don’t see the head, you can’t change anything before the given node.
Smart trick: copy the value of the next node into the current node, then delete the next node.

Step-by-step solution:

  1. node.val = node.next.val → copy the next node’s value into the current node.

  2. node.next = node.next.next → remove the next node from the list.

Quick example:

  • Input: 4 -> 5 -> 1 -> 9, node = 5

  • Step 1: 4 -> 1 -> 1 -> 9

  • Step 2: 4 -> 1 -> 9 âś…

Result: The node is deleted, the list remains correct, and you didn’t need access to the head.

C++ Code:

class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        node->next = node->next->next;
    }
};

Notes:

  • Very simple and fast: O(1) time and O(1) space.

  • This is the standard approach used in Microsoft interviews and LeetCode.

Scroll to Top