Python Program For Removing All Occurrences Of Duplicates From A Sorted Linked List Last Updated : 01 Dec, 2022 Comments Improve Suggest changes 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. Python3 # Python3 implementation for the # above approach # Creating node class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None # Add node into beginning of linked list def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node return new_node # Function to remove all occurrences # of duplicate elements def removeAllDuplicates(self, temp): # temp is head node of linkedlist curr = temp # print(' print something') head = prev = Node(None) head.next = curr # Here we use the same as we do in removing # duplicates and the only extra thing is that # we need to remove all elements # having duplicates that we did in 30-31 while curr and curr.next: # until the current value and next value # are same keep updating the current value if curr.val == curr.next.val: while(curr and curr.next and curr.val == curr.next.val): curr = curr.next # still one of duplicate values left # so point prev to curr curr = curr.next prev.next = curr else: prev = prev.next curr = curr.next return head.next # for print the linkedlist def printList(self): temp1 = self.head while temp1 is not None: print(temp1.val, end = " ") temp1 = temp1.next # For getting head of linkedlist def get_head(self): return self.head # Driver Code if __name__=='__main__': llist = LinkedList() llist.push(53) llist.push(53) llist.push(49) llist.push(49) llist.push(35) llist.push(28) llist.push(28) llist.push(23) print('Created linked list is:') llist.printList() print( 'Linked list after deletion of duplicates:') head1 = llist.get_head() #print(head1) llist.removeAllDuplicates(head1) llist.printList() # This code is contributed by PRAVEEN KUMAR(IIIT KALYANI) 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) 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 Python Program For Removing All Occurrences Of Duplicates From A Sorted Linked List kartik Follow Improve Article Tags : Linked List Python Programs DSA Python-DSA Practice Tags : Linked List Similar Reads Python 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- 7 min read Python 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 Python 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 Python 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 special pointer to remove the required node from the linked list. Python3 # 6 min read Python - Remove Duplicates from a list And Keep The Order While lists provide a convenient way to manage collections of data, duplicates within a list can sometimes pose challenges. In this article, we will explore different methods to remove duplicates from a Python list while preserving the original order.Using dict.fromkeys()dict.fromkeys() method creat 2 min read Python 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 Python Program For Removing Middle Points From a Linked List Of Line Segments Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked 4 min read Python program to remove duplicate elements index from other list Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1 7 min read Removing Duplicates of a List of Sets in Python Dealing with sets in Python allows for efficient handling of unique elements, but when working with a list of sets, you may encounter scenarios where duplicates need to be removed. In this article, we will see how we can remove duplicates of a list of sets in Python. Remove Duplicates of a List of S 2 min read Python Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l 3 min read Like