Open In App
Related Articles

Python Program For Removing All Occurrences Of Duplicates From A Sorted Linked List

Improve Article
Save Article
Like Article

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 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 This would delete the block of nodes with value 28 which has duplicates.


# Python3 implementation for the
# above approach
# Creating node
class Node:
    def __init__(self, val):
        self.val = val = 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) = 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) = 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
            # until the current value and next value
            # are same keep updating the current value
            if curr.val ==
                while(curr and and
                      curr.val ==
                    curr =
                    # still one of duplicate values left
                    # so point prev to curr
                curr =
       = curr
                prev =
                curr =
    # for print the linkedlist
    def printList(self):
        temp1 = self.head
        while temp1 is not None:
            print(temp1.val, end = " ")
            temp1 =
    # For getting head of linkedlist
    def get_head(self):
        return self.head
# Driver Code
if __name__=='__main__':
    llist = LinkedList()
    print('Created linked list is:')
    'Linked list after deletion of duplicates:')
    head1 = llist.get_head()
# This code is contributed by PRAVEEN KUMAR(IIIT KALYANI)


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!

Last Updated : 01 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials