Google News
logo
Algorithm - Interview Questions
How to delete a node in a given link list? Write an algorithm and a program?
Write a function to delete a given node from a Singly Linked List. The function must follow the following constraints :
 
* The function must accept a pointer to the start node as the first argument and node to be deleted as the second argument, i.e., a pointer to head node is not global.
* The function should not return a pointer to the head node.
* The function should not accept pointer to pointer to head node.

We may assume that the Linked List never becomes empty.
 
Suppose the function name is delNode(). In a direct implementation, the function needs to adjust the head pointer when the node to be deleted the first node.
 
C program for deleting a node in Linked List
 
We will handle the case when the first node to be deleted then we copy the data of the next node to head and delete the next node. In other cases when a deleted node is not the head node can be handled generally by finding the previous node.
#include <stdio.h>   
#include <stdlib.h>   
struct Node   
{     
    int data;   
    struct Node *next;   
};   
  
void delNode(struct Node *head, struct Node *n)   
{   
    if(head == n)   
    {     
        if(head->next == NULL)   
        {   
            printf("list can't be made empty because there is only one node. ");   
            return;   
        }   
        head->data = head->next->data;   
        n = head->next;   
        head->next = head->next->next;   
        free(n);   
        return;   
    }   
        struct Node *prev = head;   
    while(prev->next != NULL && prev->next != n)   
        prev = prev->next;   
    if(prev->next == NULL)   
    {   
        printf("\n This node is not present in  List");   
        return;   
    }   
    prev->next = prev->next->next;   
    free(n);   
    return;   
}   
void push(struct Node **head_ref, int new_data)   
{   
    struct Node *new_node =   
        (struct Node *)malloc(sizeof(struct Node));   
    new_node->data = new_data;   
    new_node->next = *head_ref;   
    *head_ref = new_node;   
}   
void printList(struct Node *head)   
{   
    while(head!=NULL)   
    {   
        printf("%d ",head->data);   
        head=head->next;   
    }   
    printf("\n");   
}   
int main()   
{   
    struct Node *head = NULL;   
    push(&head,3);   
    push(&head,2);   
    push(&head,6);   
    push(&head,5);   
    push(&head,11);   
    push(&head,10);   
    push(&head,15);   
    push(&head,12);   
    printf("Available Link list: ");   
    printList(head);   
    printf("\nDelete node %d: ", head->next->next->data);   
    delNode(head, head->next->next);   
  
    printf("\nUpdated  Linked List: ");   
    printList(head);   
  
    /* Let us delete the the first node */  
    printf("\nDelete first node ");   
    delNode(head, head);   
   
    printf("\nUpdated Linked List: ");   
    printList(head);   
  
    getchar();   
    return 0;   
}​
  
Output :
Available Link List: 12 15 10 11 5 6 2 3 
Delete node 10:
Updated Linked List: 12 15 11 5 6 2 3
Delete first node
Updated Linked list: 15 11 5 6 2 3 
Advertisement