Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to remove duplicate entries in a linked list in Python
Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list.
So, if the input is like [2 −> 4 −> 6 −> 1 −> 4 −> 6 −> 9], then the output will be [2 −> 4 −> 6 −> 1 −> 9].
Algorithm
To solve this, we will follow these steps −
- if node is not null, then
- l := a new set
- temp := node
- insert value of temp into l
- while next of temp is not null, do
- if value of next of temp is not in l, then
- insert value of next of temp into l
- temp := next of temp
- otherwise,
- next of temp := next of next of temp
- if value of next of temp is not in l, then
- return node
Implementation
Let us see the following implementation to get better understanding −
class ListNode:
def __init__(self, data, next = None):
self.val = data
self.next = next
def make_list(elements):
head = ListNode(elements[0])
for element in elements[1:]:
ptr = head
while ptr.next:
ptr = ptr.next
ptr.next = ListNode(element)
return head
def print_list(head):
ptr = head
print('[', end = "")
while ptr:
print(ptr.val, end = ", " if ptr.next else "")
ptr = ptr.next
print(']')
class Solution:
def solve(self, node):
if node:
seen = set()
temp = node
seen.add(temp.val)
while temp.next:
if temp.next.val not in seen:
seen.add(temp.next.val)
temp = temp.next
else:
temp.next = temp.next.next
return node
# Test the solution
ob = Solution()
head = make_list([2, 4, 6, 1, 4, 6, 9])
result = ob.solve(head)
print_list(result)
[2, 4, 6, 1, 9]
How It Works
The algorithm uses a set to track values we've already seen. We traverse the linked list once, and for each node:
- If the value hasn't been seen before, we add it to the set and move to the next node
- If the value is a duplicate, we skip it by updating the current node's
nextpointer to point to the node after the duplicate
This approach has O(n) time complexity and O(n) space complexity, where n is the number of nodes in the linked list.
Example with Step-by-Step Execution
class ListNode:
def __init__(self, data, next = None):
self.val = data
self.next = next
def solve_with_steps(node):
if not node:
return node
seen = set()
temp = node
seen.add(temp.val)
print(f"Added {temp.val} to seen set: {seen}")
while temp.next:
if temp.next.val not in seen:
seen.add(temp.next.val)
print(f"Added {temp.next.val} to seen set: {seen}")
temp = temp.next
else:
print(f"Duplicate {temp.next.val} found, removing it")
temp.next = temp.next.next
return node
# Create linked list: 2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9
head = ListNode(2)
head.next = ListNode(4)
head.next.next = ListNode(6)
head.next.next.next = ListNode(1)
head.next.next.next.next = ListNode(4)
head.next.next.next.next.next = ListNode(6)
head.next.next.next.next.next.next = ListNode(9)
print("Original list: 2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9")
result = solve_with_steps(head)
Original list: 2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9
Added 2 to seen set: {2}
Added 4 to seen set: {2, 4}
Added 6 to seen set: {2, 4, 6}
Added 1 to seen set: {1, 2, 4, 6}
Duplicate 4 found, removing it
Duplicate 6 found, removing it
Conclusion
This solution efficiently removes duplicates from a linked list while preserving the order of first occurrences. The use of a set provides O(1) lookup time, making the overall algorithm linear in time complexity.
