A simple solution is to traverse the linked list until you find the node you want to delete. But this solution requires a pointer to the head node which contradicts the problem statement.
The fast solution is to copy the data from the next node to the node to be deleted and delete the next node. Something like this:
struct Node *temp = node_ptr->next; node_ptr->data = temp->data; node_ptr->next = temp->next; free(temp);
Below is the implementation of the above code:
Before deleting 1 12 1 4 1 After deleting 12 1 4 1
To make this solution work we can mark the end node as a dummy node. But the programs/functions that are using this function should also be modified.
Try this problem for a doubly-linked list.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Delete a Node from linked list without head pointer
- Delete multiple occurrences of key in Linked list using double pointer
- Delete all Prime Nodes from a Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Program to delete all even nodes from a Singly Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Delete a given node in Linked List under given constraints
- Point to next higher value node in a linked list with an arbitrary pointer
- Point arbit pointer to greatest value right side node in a linked list
- Select a Random Node from a Singly Linked List
- Recursive selection sort for singly linked list | Swapping node links
- C Program to reverse each node value in Singly Linked List
- Update adjacent nodes if the current node is zero in a Singly Linked List
- Delete Nth node from the end of the given linked list
- Delete a Linked List node at a given position
- Delete a Doubly Linked List node at a given position