0% found this document useful (0 votes)
2 views

Algorithm to Delete a Node in a Singly Linked List

The document outlines an algorithm for deleting a node from a singly linked list based on its value, detailing steps for handling various cases such as deleting the head or a non-existent node. It includes C code for inserting nodes, deleting a specified node, and printing the list. The complexity analysis indicates that searching and deleting a node takes O(n), while deleting the head node is O(1).

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm to Delete a Node in a Singly Linked List

The document outlines an algorithm for deleting a node from a singly linked list based on its value, detailing steps for handling various cases such as deleting the head or a non-existent node. It includes C code for inserting nodes, deleting a specified node, and printing the list. The complexity analysis indicates that searching and deleting a node takes O(n), while deleting the head node is O(1).

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithm to Delete a Node in a Singly Linked List

Given a singly linked list, we need to delete a node based on its value.

Algorithm:

1. Check if the list is empty (head == NULL). If true, return.

2. If the node to be deleted is the head:

o Update head to head->next.

o Free the old head node.

3. Otherwise, traverse the list:

o Keep track of the previous node.

o Find the node with the given value.

o If found, update the previous node’s next to skip the node.

o Free the memory of the deleted node.

4. If the node is not found, print an appropriate message.

C Code for Deleting a Node

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

};

// Function to insert a node at the end of the list

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {

*head = newNode;

return;

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

// Function to delete a node by value

void deleteNode(struct Node** head, int key) {

struct Node* temp = *head;

struct Node* prev = NULL;

// Case 1: If the node to be deleted is the head

if (temp != NULL && temp->data == key) {

*head = temp->next;

free(temp);

return;

// Case 2: Search for the node to delete

while (temp != NULL && temp->data != key) {

prev = temp;
temp = temp->next;

// Case 3: If the node was not found

if (temp == NULL) {

printf("Node with value %d not found.\n", key);

return;

// Case 4: Unlink the node and free memory

prev->next = temp->next;

free(temp);

// Function to print the linked list

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

// Main function

int main() {

struct Node* head = NULL;

// Insert nodes
insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

insertAtEnd(&head, 30);

insertAtEnd(&head, 40);

printf("Original List: ");

printList(head);

// Delete a node

deleteNode(&head, 20);

printf("After Deleting 20: ");

printList(head);

// Delete a non-existent node

deleteNode(&head, 50);

// Delete the head node

deleteNode(&head, 10);

printf("After Deleting Head (10): ");

printList(head);

return 0;

Explanation

1. insertAtEnd

o Inserts nodes at the end.

2. deleteNode

o Handles different cases:


▪ If the list is empty.

▪ If the node is the head.

▪ If the node is found in the middle or end.

▪ If the node is not found.

o Frees memory after deletion.

3. printList

o Prints the linked list.

4. Main Function

o Inserts nodes.

o Deletes specific nodes.

o Handles edge cases.

Complexity Analysis

• Searching for a node: O(n)

• Deleting a node: O(n)

• Deleting the head node: O(1)

You might also like