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
Python program to remove duplicate elements from a Doubly Linked List
When it is required to remove duplicate elements in a doubly linked list, a Node class needs to be created. In this class, there are three attributes: the data that is present in the node, the access to the next node, and the access to the previous node of the linked list.
Below is a demonstration for the same ?
Node Class Structure
The Node class represents each element in the doubly linked list ?
class Node:
def __init__(self, my_data):
self.previous = None
self.data = my_data
self.next = None
Doubly Linked List Implementation
The complete implementation with duplicate removal functionality ?
class Node:
def __init__(self, my_data):
self.previous = None
self.data = my_data
self.next = None
class double_list:
def __init__(self):
self.head = None
self.tail = None
def add_data(self, my_data):
new_node = Node(my_data)
if(self.head == None):
self.head = self.tail = new_node
self.head.previous = None
self.tail.next = None
else:
self.tail.next = new_node
new_node.previous = self.tail
self.tail = new_node
self.tail.next = None
def print_it(self):
curr = self.head
if (self.head == None):
print("The list is empty")
return
print("The nodes in the doubly linked list are :")
while curr != None:
print(curr.data)
curr = curr.next
def remove_duplicates(self):
if(self.head == None):
return
else:
curr = self.head
while(curr != None):
index_val = curr.next
while(index_val != None):
if(curr.data == index_val.data):
temp = index_val
index_val.previous.next = index_val.next
if(index_val.next != None):
index_val.next.previous = index_val.previous
index_val = index_val.next
temp = None
else:
index_val = index_val.next
curr = curr.next
# Create instance and demonstrate functionality
my_instance = double_list()
print("Elements are being added to the doubly linked list")
my_instance.add_data(10)
my_instance.add_data(24)
my_instance.add_data(54)
my_instance.add_data(77)
my_instance.add_data(24)
my_instance.print_it()
print("The elements in the list after removing duplicates are : ")
my_instance.remove_duplicates()
my_instance.print_it()
Elements are being added to the doubly linked list The nodes in the doubly linked list are : 10 24 54 77 24 The elements in the list after removing duplicates are : The nodes in the doubly linked list are : 10 24 54 77
How the Algorithm Works
The duplicate removal algorithm uses a nested loop approach ?
- The outer loop traverses each node starting from the head
- The inner loop checks all subsequent nodes for duplicates
- When a duplicate is found, it updates the previous and next pointers to bypass the duplicate node
- The duplicate node is removed from memory
Key Points
- The Node class contains three attributes:
data,next, andprevious - The
add_data()method adds new nodes at the end of the list - The
remove_duplicates()method compares each node with all subsequent nodes - When removing a node, both forward and backward links must be updated
- Time complexity is O(n²) due to the nested loop structure
Conclusion
This implementation successfully removes duplicate elements from a doubly linked list by comparing each node with all subsequent nodes. The algorithm maintains the doubly linked structure by properly updating both forward and backward pointers when removing duplicates.
