Given a liked list and a key to be deleted. Delete last occurrence of key from linked. The list may have duplicates.
Input: 1->2->3->5->2->10, key = 2 Output: 1->2->3->5->10
The idea is to traverse the linked list from beginning to end. While traversing, keep track of last occurrence key. After traversing the complete list, delete last occurrence by copying data of next node and deleting the next node.
Created Linked List: 1 2 3 5 2 10 Linked List after Deletion of 1: 1 2 3 5 10
The above solution doesn’t work when the node to be deleted is the last node.
Following solution handles all cases.
Created Linked List: 1 2 3 4 5 4 4 Linked List after Deletion of 1: 1 2 3 4 5 4
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Find the middle of a given linked list in C and Java
- Program for n'th node from the end of a Linked List
- Write a function to get Nth node in a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Detect loop in a linked list
- Write a function to delete a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Reverse a linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Write a function to get the intersection point of two Linked Lists.
- Function to check if a singly linked list is palindrome
- The Great Tree-List Recursion Problem.
- Clone a linked list with next and random pointer | Set 1
- Memory efficient doubly linked list
- Given a linked list which is sorted, how will you insert in sorted way