Delete Node in a Linked List - Problem

Imagine you're given a mysterious mission: delete a specific node from a singly-linked list, but here's the twist - you don't have access to the head of the list!

You are given only the node to be deleted, and it's guaranteed that this node is not the last node in the list. All values in the linked list are unique.

Your goal: Make this node "disappear" from the linked list without having access to the previous nodes or the head. The list should maintain its structure and order, just without the target node.

Note: We don't mean removing it from memory completely - we mean the node's value should no longer exist in the logical structure of the list, and the list length should decrease by one.

Example: If the list is 4 -> 5 -> 1 -> 9 and you need to delete node with value 5, the result should be 4 -> 1 -> 9.

Input & Output

example_1.py โ€” Basic Deletion
$ Input: head = [4,5,1,9], node = 5
โ€บ Output: [4,1,9]
๐Ÿ’ก Note: We're given the node with value 5. We copy the next node's value (1) to this node, making it [4,1,1,9], then skip the next node to get [4,1,9]. The value 5 has been effectively removed.
example_2.py โ€” Middle Node Deletion
$ Input: head = [4,5,1,9], node = 1
โ€บ Output: [4,5,9]
๐Ÿ’ก Note: We're given the node with value 1. We copy the next node's value (9) to this node, making it [4,5,9,9], then skip the next node to get [4,5,9]. The original value 1 has been removed.
example_3.py โ€” Two Node List
$ Input: head = [1,2], node = 1
โ€บ Output: [2]
๐Ÿ’ก Note: In a two-node list, we delete the first node by copying the second node's value (2) to the first node, then skipping the second node. Result is just [2].

Constraints

  • The number of nodes in the given list is in the range [2, 1000]
  • -1000 <= Node.val <= 1000
  • The value of each node in the list is unique
  • The node to be deleted is in the list and is not a tail node

Visualization

Tap to expand
๐ŸŽญ The Identity Swap Magic TrickStep 1: ProblemNeed to delete nodebut can't access previousStep 2: CopyCopy next node's valueto current nodeStep 3: SkipUpdate pointer toskip next nodeStep 4: Done!Original valueeffectively deletedBefore: [4] -> [5] -> [1] -> [9]5DELETE THISAfter: [4] -> [1] -> [9]1NOW CONTAINS 1๐ŸŽฏ The node physically stays, but its value changes!We've effectively "moved" the next node to this position.
Understanding the Visualization
1
The Problem
You need to delete yourself from a line, but you can't leave your position and can't ask people in front to help
2
The Trick
Copy the identity (name tag) of the person behind you
3
The Swap
Now you have their identity, and you can make them leave instead
4
Mission Complete
Your original identity has vanished from the line!
Key Takeaway
๐ŸŽฏ Key Insight: When you can't delete a node traditionally, make it become the next node and delete that one instead!
Asked in
Adobe 15 Apple 12 Microsoft 8 Google 6
125.0K Views
Medium Frequency
~8 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen