Delete a Given Node

Delete node when only node reference is given (no head access)

Read Here
Step0 / 5
PhaseInitialize
Nodes5
Deleted2
Back To Linked List

Code

1def deleteNode(node):
2 # Copy the next node's value to current node
3 node.val = node.next.val
4
5 # Skip over the next node
6 node.next = node.next.next
Function definition

Linked List

INIT

Step

0 / 5

Phase

Initialize

Current

2

null1next2next3next4next5nextcurrnext

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.