C++ Program For Removing All Occurrences Of Duplicates From A Sorted Linked List Last Updated : 03 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. Examples: 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 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. C++ // C++ program to remove all occurrences // of duplicates from a sorted linked list. #include <bits/stdc++.h> using namespace std; // A linked list node struct Node { int data; struct Node *next; }; // Utility function // to create a new Node struct Node *newNode(int data) { Node *temp = new Node; temp -> data = data; temp -> next = NULL; return temp; } // Function to print nodes // in a given linked list. void printList(struct Node *node) { while (node != NULL) { printf("%d ", node -> data); node = node -> next; } } // Function to remove all occurrences // of duplicate elements void removeAllDuplicates(struct Node* &start) { // Create a dummy node that acts like // a fake head of list pointing to the // original head Node* dummy = new Node; // Dummy node points to the original head dummy -> next = start; // Node pointing to last node which has // no duplicate. Node* prev = dummy; // Node used to traverse the linked list. Node* current = start; while(current != NULL) { // Until the current and previous // values are same, keep updating current while(current -> next != NULL && prev -> next -> data == current -> next -> data) current = current -> next; // if current has unique value // i.e current is not updated, // Move the prev pointer to // next node if (prev -> next == current) prev = prev -> next; // When current is updated to the // last duplicate value of that segment, // make prev the next of current else prev -> next = current -> next; current = current -> next; } // Update original head to the next // of dummy node start = dummy -> next; } // Driver Code int main() { // 23->28->28->35->49->49->53->53 struct Node* start = newNode(23); start -> next = newNode(28); start -> next -> next = newNode(28); start -> next -> next -> next = newNode(35); start -> next -> next -> next -> next = newNode(49); start -> next -> next -> next -> next -> next = newNode(49); start -> next -> next -> next -> next -> next -> next = newNode(53); start -> next -> next -> next -> next -> next -> next -> next = newNode(53); cout << "List before removal " << "of duplicates"; printList(start); removeAllDuplicates(start); cout << "List after removal " << "of duplicates"; printList(start); return 0; } // This code is contributed by NIKHIL JINDAL Output: List before removal of duplicates 23 28 28 35 49 49 53 53 List after removal of duplicates 23 35 Time Complexity: O(n) Auxiliary Space: O(1) because using constant variables Please refer complete article on Remove all occurrences of duplicates from a sorted Linked List for more details! Comment More infoAdvertise with us Next Article C++ Program For Removing Duplicates From A Sorted Linked List K kartik Follow Improve Article Tags : Linked List C++ Programs DSA Practice Tags : Linked List Similar Reads C++ Program For Removing Duplicates From A Sorted Linked List Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43- 9 min read C++ Program For Removing Duplicates From An Unsorted Linked List Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43. Recommended: 4 min read C++ Program For Moving All Occurrences Of An Element To End In A Linked List Given a linked list and a key in it, the task is to move all occurrences of the given key to the end of the linked list, keeping the order of all other elements the same. Examples: Input : 1 -> 2 -> 2 -> 4 -> 3 key = 2 Output : 1 -> 4 -> 3 -> 2 -> 2 Input : 6 -> 6 -> 7 6 min read C++ Program For Deleting Last Occurrence Of An Item From Linked List Using pointers, loop through the whole list and keep track of the node prior to the node containing the last occurrence key using a special pointer. After this just store the next of next of the special pointer, into to next of special pointer to remove the required node from the linked list. C++ // 6 min read C++ Program For Removing Every K-th Node Of The Linked List Given a singly linked list, Your task is to remove every K-th node of the linked list. Assume that K is always less than or equal to length of Linked List.Examples : Input: 1->2->3->4->5->6->7->8 k = 3 Output: 1->2->4->5->7->8 As 3 is the k-th node after its deletion list would be 1->2->4->5->6->7-> 3 min read C++ Program To Remove Duplicates From Sorted Array Given a sorted array, the task is to remove the duplicate elements from the array.Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} new size = 1 Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} new size = 5 Recommended PracticeRemove duplicate elements from sorte 3 min read Like