C Program For Deleting A Given Node In Linked List Under Given Constraints
Last Updated :
22 Jun, 2022
Given a Singly Linked List, write a function to delete a given node. Your function must follow the following constraints:
- It must accept a pointer to the start node as the first parameter and node to be deleted as the second parameter i.e., a pointer to the head node is not global.
- It should not return a pointer to the head node.
- It should not accept pointer to pointer to the head node.
You may assume that the Linked List never becomes empty.
Let the function name be deleteNode(). In a straightforward implementation, the function needs to modify the head pointer when the node to be deleted is the first node. As discussed in previous post, when a function modifies the head pointer, the function must use one of the given approaches, we can’t use any of those approaches here.
Solution
We explicitly handle the case when the node to be deleted is the first node, we copy the data of the next node to head and delete the next node. The cases when a deleted node is not the head node can be handled normally by finding the previous node and changing next of the previous node. The following are the implementation.
C
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void deleteNode( struct Node *head,
struct Node *n)
{
if (head == n)
{
if (head->next == NULL)
{
printf ( "There is only one node. " ,
"The list can't be made empty " );
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 (
"Given node is not present in Linked 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 ( "" );
}
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 ( "Given Linked List: " );
printList(head);
printf ("
Deleting node %d: ",
head->next->next->data);
deleteNode(head,
head->next->next);
printf (
"Modified Linked List: " );
printList(head);
printf (
"Deleting first node " );
deleteNode(head, head);
printf (
"Modified Linked List: " );
printList(head);
getchar ();
return 0;
}
|
Output:
Given Linked List: 12 15 10 11 5 6 2 3
Deleting node 10:
Modified Linked List: 12 15 11 5 6 2 3
Deleting first node
Modified Linked List: 15 11 5 6 2 3
Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Please refer complete article on Delete a given node in Linked List under given constraints for more details!
Share your thoughts in the comments
Please Login to comment...