Delete a Given Node
Delete node when only node reference is given (no head access)
Code
1def deleteNode(node):2 # Copy the next node's value to current node3 node.val = node.next.val45 # Skip over the next node6 node.next = node.next.next
Function definition
Linked List
INIT
Step
0 / 5
Phase
Initialize
Current
2
Progress
Processing
Deleting node by copying next value and skipping
Pointer State
Pointers
current
2
next
3
nextNext
4
Key Insight
Since we don't have access to the previous node, we copy the next node's value and skip over it instead.
Explanation
Init
Initialize Delete
Node with value 2 is marked for deletion.
💡 Tip
We only have access to this node, not the head.