
Delete a Node from Linked List (DSA) – Complete Guide with Examples
🚀 Introduction
Deleting a node in a Linked List means removing a specific node and updating the links (pointers) so the list remains connected.
👉 Unlike arrays, no shifting is required — just pointer updates.
🔹 Linked List Structure
[Data | Next] → [Data | Next] → [Data | Next] → NULL
🔹 Ways to Delete a Node
- Delete by Value
- Delete by Position
- Delete First Node
- Delete Last Node
🔹 1. Delete First Node
💡 Concept
- Move
headto next node
🧾 Example
Before:
10 → 20 → 30 → NULL
After:
20 → 30 → NULL
⚡ Complexity
👉 O(1)
🔹 2. Delete Last Node
💡 Concept
- Traverse to second last node
- Set
next = NULL
🧾 Example
Before:
10 → 20 → 30 → NULL
After:
10 → 20 → NULL
⚡ Complexity
👉 O(n)
🔹 3. Delete by Value
💡 Concept
- Find node with given value
- Link previous node to next
🧾 Example
Before:
10 → 20 → 30 → 40 → NULL
Delete value = 30
After:
10 → 20 → 40 → NULL
⚡ Complexity
👉 O(n)
🔹 4. Delete by Position
💡 Concept
- Traverse to position – 1
- Skip the node
🧾 Example
Before:
10 → 20 → 30 → 40 → NULL
Delete position 2
After:
10 → 30 → 40 → NULL
⚡ Complexity
👉 O(n)
⚙️ Algorithm (Delete by Value)
1. If head is NULL → return
2. If head.data == value → head = head.next
3. Traverse list:
if temp.next.data == value:
temp.next = temp.next.next
break
🧑💻 PHP Implementation
<?phpclass Node {
public $data;
public $next; public function __construct($data) {
$this->data = $data;
$this->next = null;
}
}class LinkedList {
public $head; // Delete by value
public function deleteByValue($value) {
if ($this->head == null) return; // If head is to be deleted
if ($this->head->data == $value) {
$this->head = $this->head->next;
return;
} $temp = $this->head; while ($temp->next != null) {
if ($temp->next->data == $value) {
$temp->next = $temp->next->next;
return;
}
$temp = $temp->next;
}
} // Display
public function display() {
$temp = $this->head;
while ($temp != null) {
echo $temp->data . " → ";
$temp = $temp->next;
}
echo "NULL";
}
}?>
⚡ Time Complexity Summary
| Operation | Complexity |
|---|---|
| Delete First Node | O(1) |
| Delete Last Node | O(n) |
| Delete by Value | O(n) |
| Delete by Position | O(n) |
🎯 Key Points
✅ No shifting required
✅ Pointer manipulation is key
✅ Efficient for dynamic data
🧠 Interview Tips
👉 Important edge cases:
- Empty list
- Single node
- Value not found
👉 Common questions:
- Delete nth node from end
- Delete node without head
- Remove duplicates
🏁 Conclusion
Deleting a node is a core Linked List operation.
👉 Mastering this helps you:
- Understand pointer logic
- Solve advanced DSA problems
- Crack coding interviews
No comments yet! You be the first to comment.
