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
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 the implementation of above idea.
Created Linked List: 8 2 3 1 7 Linked List after Deletion at position 4: 8 2 3 1
Thanks to Hemanth Kumar for suggesting initial solution. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Delete a Doubly Linked List node at a given position
- Delete Nth node from the end of the given linked list
- Delete a given node in Linked List under given constraints
- Delete a node in a Doubly Linked List
- Delete every Kth node from circular linked list
- Insert a node at a specific position in a linked list
- Delete a Node from linked list without head pointer
- Recursive function to delete k-th node from linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Create a linked list from two linked lists by choosing max element at each position
- Create new linked list from two given linked list with greater element at each node
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Insert a whole linked list into other at k-th position
- Delete all occurrences of a given key in a linked list