Given a singly linked list and a position, delete a linked list node at the given position.
Input: position = 1, Linked List = 8->2->3->1->7
Output: Linked List = 8->3->1->7
Input: position = 0, Linked List = 8->2->3->1->7
Output: Linked List = 2->3->1->7
We strongly recommend you to minimize your browser and try this yourself first If node to be deleted is root, simply delete it. To delete a middle node, we must have pointer to the node previous to the node to be deleted. So if positions is not zero, we run a loop position-1 times and get pointer to the previous node.
Below is C implementation of above idea.
// A complete working C program to delete a node in a linked list
// at a given position
// A linked list node
/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */