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 the node to be deleted is the root, simply delete it. To delete a middle node, we must have a pointer to the node previous to the node to be deleted. So if positions are not zero, we run a loop position-1 times and get a pointer to the previous node.
Below is the implementation of the 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
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.
- 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 every Kth node from circular linked list
- Delete a node in a Doubly Linked List
- Delete a Node from linked list without head pointer
- Recursive function to delete k-th node from linked list
- Insert a node at a specific position in a 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
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Create new linked list from two given linked list with greater element at each node
- Delete a linked list using recursion
- Delete middle of linked list
- Delete all occurrences of a given key in a linked list
- Insert a whole linked list into other at k-th position
- Delete continuous nodes with sum K from a given linked list
- Delete all occurrences of a given key in a doubly linked list
- Delete last occurrence of an item from linked list