✅ Deletion in Linked List
✅ 1. Introduction
Deletion in a linked list means removing an existing node from the list.
Unlike arrays, deletion in a linked list does not require shifting of elements; only pointer adjustments are needed.
✅ 2. Types of Deletion in Linked List
Deletion in a Singly Linked List can be performed at:
- Deletion at Beginning
- Deletion at End
- Deletion at a Specific Position (Middle)
✅ 3. Deletion at Beginning
✅ Definition
Deletion at beginning means removing the first node of the linked list.
✅ Steps Involved
- Check if
HEAD == NULL(list empty) - Store address of first node in
temp - Move
HEADto the next node - Free the memory of
temp
✅ Diagram
Before deletion:
HEAD → 10 → 20 → 30 → NULL
After deleting first node:
HEAD → 20 → 30 → NULL
✅ Algorithm: Delete_Begin(HEAD)
- Start
- If
HEAD == NULL, print List is empty and Stop - Set
temp = HEAD - Set
HEAD = HEAD->next - Free
temp - Stop
✅ Time Complexity
✅ O(1)
✅ 4. Deletion at End
✅ Definition
Deletion at end means removing the last node of the linked list.
✅ Steps Involved
- Check if list is empty
- Traverse to the second last node
- Set second last node’s
next = NULL - Free the last node
✅ Diagram
Before deletion:
HEAD → 10 → 20 → 30 → NULL
After deleting last node:
HEAD → 10 → 20 → NULL
✅ Algorithm: Delete_End(HEAD)
- Start
- If
HEAD == NULL, print List is empty and Stop - If
HEAD->next == NULL(only one node):- Free
HEAD - Set
HEAD = NULL - Stop
- Free
- Set
temp = HEAD - While
temp->next->next != NULLtemp = temp->next
- Free
temp->next - Set
temp->next = NULL - Stop
✅ Time Complexity
✅ O(n)
✅ 5. Deletion at a Specific Position (Middle)
✅ Definition
Deletion at a specific position means removing a node from a given position in the linked list.
✅ Steps Involved
- Check if list is empty
- If position is 1, perform deletion at beginning
- Traverse to the node just before the position
- Store address of node to be deleted
- Adjust pointer to skip the deleted node
- Free the deleted node
✅ Diagram
Delete node at position 3:
Before:
HEAD → 10 → 20 → 30 → 40 → NULL
After:
HEAD → 10 → 20 → 40 → NULL
✅ Algorithm: Delete_Position(HEAD, POS)
- Start
- If
HEAD == NULL, print List is empty and Stop - If
POS == 1, perform Delete_Begin and Stop - Set
temp = HEAD - Repeat
POS-2times:temp = temp->next
- Set
del = temp->next - Set
temp->next = del->next - Free
del - Stop
✅ Time Complexity
✅ O(n)
✅ 6. Special Case: Deletion in Circular Linked List (Brief)
- Need to update last node’s
nextpointer. - No NULL pointer, so careful traversal required.
✅ 7. Advantages of Deletion in Linked List
✅ Benefits:
- No shifting of elements
- Efficient compared to arrays
- Dynamic memory handling
✅ Conclusion
Deletion in a linked list involves removing a node by adjusting pointers. Deletion at the beginning is the fastest (O(1)), while deletion at the end and at a specific position requires traversal (O(n)). Linked lists are efficient when frequent deletions are required.
