Code With Coffie
  • HOME
  • ABOUT US
  • PORTFOLIO
  • JAVASCRIPT
    • Vue.js
  • PHP
    • PHP OOPS
    • LARAVEL
    • WORDPRESS
  • MYSQL
    • DATETIME
  • DSA
    • LEETCODE
  • Home
  • Blog
  • Uncategorized
  • Delete a Node from Linked List (DSA) – Complete Guide with Examples
Delete a Node from Linked List

Delete a Node from Linked List (DSA) – Complete Guide with Examples

Mar 22, 2026 by codewithhemu

🚀 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

  1. Delete by Value
  2. Delete by Position
  3. Delete First Node
  4. Delete Last Node

🔹 1. Delete First Node

💡 Concept

  • Move head to 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

OperationComplexity
Delete First NodeO(1)
Delete Last NodeO(n)
Delete by ValueO(n)
Delete by PositionO(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
  • Share:
Previous Article Deletion in Linked List (DSA) – Delete Node at Beginning, End & Position
Next Article Circular Linked List in Data Structures – Operations, Examples & Code
No comments yet! You be the first to comment.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

category

  • DATETIME (6)
  • DJANGO (1)
  • Docker (1)
  • DSA (21)
  • DSA PRACTICE (4)
  • ENGLISH READING (1)
  • JAVASCRIPT (69)
  • LARAVEL (40)
  • LeetCode (1)
  • MYSQL (45)
  • PHP (21)
  • PHP OOPS (16)
  • PROGRAMME (1)
  • PYTHON (7)
  • REACT JS (6)
  • STAR PATTERN PROGRAMME (7)
  • Uncategorized (20)
  • Vue.js (5)
  • WORDPRESS (15)

Archives

  • March 2026
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • January 2023

Tags

Certificates Education Instructor Languages School Member

Building reliable software solutions for modern businesses. Sharing practical tutorials and real-world project insights to help developers grow with confidence.

GET HELP

  • Home
  • Portfolio
  • Privacy Policy
  • Terms & Conditions
  • Disclaimer
  • Contact Us

PROGRAMS

  • Software Development
  • Performance Optimization
  • System Architecture
  • Project Consultation
  • Technical Mentorship

CONTACT US

  • Netaji Subhash Place (NSP) Delhi
  • Tel: + (91) 8287315524
  • Email: contact@codewithcoffie.com

Copyright © 2026 LearnPress LMS | Powered by LearnPress LMS