Given a Singly Linked List, write a function to delete a given node.
Read full article from Delete a given node in Linked List under given constraints | GeeksforGeeks
void
deleteNode(
struct
node *head,
struct
node *n)
{
// When node to be deleted is head node
if
(head == n)
{
if
(head->next == NULL)
{
printf
(
"There is only one node. The list can't be made empty "
);
return
;
}
/* Copy the data of next node to head */
head->data = head->next->data;
// store address of next node
n = head->next;
// Remove the link of next node
head->next = head->next->next;
// free memory
free
(n);
return
;
}
// When not first node, follow the normal deletion process
// find the previous node
struct
node *prev = head;
while
(prev->next != NULL && prev->next != n)
prev = prev->next;
// Check if node really exists in Linked List
if
(prev->next == NULL)
{
printf
(
"\n Given node is not present in Linked List"
);
return
;
}
// Remove node from Linked List
prev->next = prev->next->next;
// Free memory
free
(n);
return
;
}