Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the deletion of element in linked list
Linked lists use dynamic memory allocation which allows them to grow and shrink during runtime. A linked list is a collection of nodes where each node contains two parts: data (stores the value) and link (points to the next node).
The main operations performed on linked lists include insertion, deletion, and traversing.
Syntax
void deleteNode(struct Node **head_ref, int position);
Deletion Operations
Deletion in a linked list can be performed at different positions −
Method 1: Delete at Beginning (Position 0)
When deleting the first node, update the head pointer to point to the second node and free the memory of the deleted node.
Method 2: Delete at Middle Position
To delete a node at a specific position, traverse to the node before the target position, update its link to skip the target node, and free the target node's memory.
Method 3: Delete at End
For deleting the last node, traverse to the second-last node, set its link to NULL, and free the last node's memory.
Example: Complete Deletion Program
Following is a complete C program demonstrating deletion at any position in a linked list −
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Memory allocation failed<br>");
return;
}
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct Node **head_ref, int position) {
if (*head_ref == NULL) {
printf("List is empty<br>");
return;
}
struct Node* temp = *head_ref;
/* Delete first node */
if (position == 0) {
*head_ref = temp->next;
free(temp);
return;
}
/* Find node before the one to be deleted */
for (int i = 0; temp != NULL && i < position - 1; i++)
temp = temp->next;
/* Position is beyond the list length */
if (temp == NULL || temp->next == NULL) {
printf("Position out of bounds<br>");
return;
}
struct Node *nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
free(nodeToDelete);
}
void printList(struct Node *node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("<br>");
}
int main() {
struct Node* head = NULL;
/* Create linked list: 8 -> 2 -> 3 -> 1 -> 7 */
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
push(&head, 8);
printf("Original List: ");
printList(head);
deleteNode(&head, 2);
printf("After deleting position 2: ");
printList(head);
deleteNode(&head, 0);
printf("After deleting position 0: ");
printList(head);
return 0;
}
Original List: 8 2 3 1 7 After deleting position 2: 8 2 1 7 After deleting position 0: 2 1 7
Key Points
- Always check if the list is empty before attempting deletion
- Free memory of the deleted node to prevent memory leaks
- Update pointers correctly to maintain list integrity
- Handle edge cases like deleting the first node or invalid positions
Time Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Delete at beginning | O(1) | O(1) |
| Delete at position | O(n) | O(1) |
Conclusion
Deletion in linked lists requires careful pointer manipulation and memory management. Always validate positions and free allocated memory to maintain program efficiency and prevent memory leaks.
