Python Program For Removing Duplicates From An Unsorted Linked List
Last Updated :
13 Apr, 2023
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.
METHOD 1 (Using two loops):
This is the simple way where two loops are used. Outer loop is used to pick the elements one by one and the inner loop compares the picked element with the rest of the elements.
Thanks to Gaurav Saxena for his help in writing this code.
Python3
# Python3 program to remove duplicates
# from unsorted linked list
class Node():
def __init__(self, data):
self.data = data
self.next = None
class LinkedList():
def __init__(self):
# Head of list
self.head = None
def remove_duplicates(self):
ptr1 = None
ptr2 = None
dup = None
ptr1 = self.head
# Pick elements one by one
while (ptr1 != None and
ptr1.next != None):
ptr2 = ptr1
# Compare the picked element with
# rest of the elements
while (ptr2.next != None):
# If duplicate then delete it
if (ptr1.data == ptr2.next.data):
# Sequence of steps is important here
dup = ptr2.next
ptr2.next = ptr2.next.next
else:
ptr2 = ptr2.next
ptr1 = ptr1.next
# Function to print nodes in a
# given linked list
def printList(self):
temp = self.head
while(temp != None):
print(temp.data, end = " ")
temp = temp.next
print()
# Driver code
list = LinkedList()
list.head = Node(10)
list.head.next = Node(12)
list.head.next.next = Node(11)
list.head.next.next.next = Node(11)
list.head.next.next.next.next = Node(12)
list.head.next.next.next.next.next = Node(11)
list.head.next.next.next.next.next.next = Node(10)
print("Linked List before removing duplicates :")
list.printList()
list.remove_duplicates()
print()
print("Linked List after removing duplicates :")
list.printList()
# This code is contributed by maheshwaripiyush9
Output:
Linked list before removing duplicates:
10 12 11 11 12 11 10
Linked list after removing duplicates:
10 12 11
Time Complexity: O(n^2)
Auxiliary Space: O(1)
METHOD 2 (Use Sorting):
In general, Merge Sort is the best-suited sorting algorithm for sorting linked lists efficiently.
1) Sort the elements using Merge Sort. We will soon be writing a post about sorting a linked list. O(nLogn)
2) Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n)
Please note that this method doesn't preserve the original order of elements.
Time Complexity: O(nLogn)
Auxiliary Space :O(1)
METHOD 3 (Use Hashing):
We traverse the link list from head to end. For every newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.
Python3
# Python3 program to remove duplicates
# from unsorted linkedlist
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
# Function to print nodes in a
# given linked list
def printlist(self):
temp = self.head
while (temp):
print(temp.data, end = " ")
temp = temp.next
# Function to remove duplicates from a
# unsorted linked list
def removeDuplicates(self, head):
# Base case of empty list or
# list with only one element
if self.head is None or self.head.next is None:
return head
# Hash to store seen values
hash = set()
current = head
hash.add(self.head.data)
while current.next is not None:
if current.next.data in hash:
current.next = current.next.next
else:
hash.add(current.next.data)
current = current.next
return head
# Driver code
if __name__ == "__main__":
# Creating Empty list
llist = LinkedList()
llist.head = Node(10)
second = Node(12)
third = Node(11)
fourth = Node(11)
fifth = Node(12)
sixth = Node(11)
seventh = Node(10)
# Connecting second and third
llist.head.next = second
second.next = third
third.next = fourth
fourth.next = fifth
fifth.next = sixth
sixth.next = seventh
# Printing data
print("Linked List before removing Duplicates.")
llist.printlist()
llist.removeDuplicates(llist.head)
print("Linked List after removing duplicates.")
llist.printlist()
# This code is contributed by rajataro0
Output:
Linked list before removing duplicates:
10 12 11 11 12 11 10
Linked list after removing duplicates:
10 12 11
Thanks to bearwang for suggesting this method.
Time Complexity: O(n) on average (assuming that hash table access time is O(1) on average).
Auxiliary Space: O(n), for using the HashSet.
Please refer complete article on Remove duplicates from an unsorted linked list for more details!
Similar Reads
Javascript Program For Removing Duplicates From An Unsorted Linked List Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 ->
5 min read
Javascript 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-
8 min read
Remove Duplicates from an Unsorted Linked List Given an unsorted linked list containing n nodes, the task is to remove duplicate nodes while preserving the original order.Examples:Input: 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: The second occurrence of 12 (the one after
14 min read
Ways to remove duplicates from list in Python In this article, we'll learn several ways to remove duplicates from a list in Python. The simplest way to remove duplicates is by converting a list to a set.Using set()We can use set() to remove duplicates from the list. However, this approach does not preserve the original order.Pythona = [1, 2, 2,
2 min read
Program to print duplicates from a list of integers in Python In this article, we will explore various methods to print duplicates from a list of integers in Python. The simplest way to do is by using a set.Using SetSet in Python only stores unique elements. So, we loop through the list and check if the current element already exist (duplicate) in the set, if
2 min read
Python | Remove consecutive duplicates from list Removing consecutive duplicates from a list means eliminating repeated elements that appear next to each other in the list. If an element repeats consecutively, only the first occurrence should remain and the duplicates should be removed.Example:Input: ['a', 'a', 'b', 'b', 'c', 'a', 'a', 'a']Output:
3 min read