Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list.
Input : 23->28->28->35->49->49->53->53 Output : 23->35 Input : 11->11->11->11->75->75 Output : empty List
Note that this is different from Remove Duplicates From Linked List
The idea is to maintain a pointer (prev) to the node which just previous to the block of nodes we are checking for duplicates. In the first example, the pointer prev would point to 23 while we check for duplicates for the node 28. Once we reach the last duplicate node with value 28 (name it current pointer), we can make the next field of prev node to be the next of current and update current=current.next. This would delete the block of nodes with value 28 which has duplicates.
List before removal of duplicates 23 28 28 35 49 49 53 53 List after removal of duplicates 23 35
Time Complexity : O(n)
This article is contributed by Saloni Baweja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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.
- Remove duplicates from a sorted doubly linked list
- Remove duplicates from a sorted linked list using recursion
- Remove duplicates from a sorted linked list
- Merge two sorted linked list without duplicates
- Remove duplicates from an unsorted doubly linked list
- Remove duplicates from an unsorted linked list
- Move all occurrences of an element to end in a linked list
- Delete all occurrences of a given key in a doubly linked list
- Delete all occurrences of a given key in a linked list
- Given a linked list which is sorted, how will you insert in sorted way
- Insert value in sorted way in a sorted doubly linked list
- Merge K sorted Doubly Linked List in Sorted Order
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Replace nodes with duplicates in linked list
- Count duplicates in a given linked list
- Delete multiple occurrences of key in Linked list using double pointer
- Sorted merge of two sorted doubly circular linked lists
- Remove all special characters from a singly Linked List
- Remove all nodes from a Doubly Linked List containing Fibonacci numbers
- Remove all Fibonacci Nodes from a Circular Singly Linked List