In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list is a data structure consisting of nodes, where each node contains a value and a reference (or link) to the next node in the list.
Problem Statement
Given a linked list, remove any duplicate elements so that only the distinct elements remain in the list.
Python Program to Remove Duplicates from a Linked List
# Introduction # In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list is a data structure consisting of nodes, where each node contains a value and a reference (or link) to the next node in the list. # Problem Statement # Given a linked list, remove any duplicate elements so that only the distinct elements remain in the list. # Program class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return current = self.head while current.next: current = current.next current.next = new_node def display(self): current = self.head while current: print(current.data, end=' ') current = current.next print() def remove_duplicates(self): current = self.head while current: runner = current while runner.next: if runner.next.data == current.data: runner.next = runner.next.next else: runner = runner.next current = current.next # How it Works # 1. We define two classes, Node and LinkedList. Node represents an element in the linked list, and LinkedList is used to manage the list. # 2. The `append` method is used to add elements to the linked list. # 3. The `display` method is used to print the elements of the linked list. # 4. The `remove_duplicates` method is used to remove duplicate elements from the linked list. # 5. We iterate through the list, and for each element, we iterate through the rest of the list to remove any duplicates. # Input/Output if __name__ == "__main__": linked_list = LinkedList() elements = [1, 2, 2, 3, 4, 4, 5] for element in elements: linked_list.append(element) print("Original Linked List:") linked_list.display() linked_list.remove_duplicates() print("\nLinked List after removing duplicates:") linked_list.display()
Python
​x
66
1
# Introduction
2
# In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list is a data structure consisting of nodes, where each node contains a value and a reference (or link) to the next node in the list.
3
​
4
# Problem Statement
5
# Given a linked list, remove any duplicate elements so that only the distinct elements remain in the list.
6
​
7
# Program
8
class Node:
9
def __init__(self, data):
10
self.data = data
11
self.next = None
12
​
13
class LinkedList:
14
def __init__(self):
15
self.head = None
16
​
17
def append(self, data):
18
new_node = Node(data)
19
if self.head is None:
20
self.head = new_node
21
return
22
current = self.head
23
while current.next:
24
current = current.next
25
current.next = new_node
26
​
27
def display(self):
28
current = self.head
29
while current:
30
print(current.data, end=' ')
31
current = current.next
32
print()
33
​
34
def remove_duplicates(self):
35
current = self.head
36
while current:
37
runner = current
38
while runner.next:
39
if runner.next.data == current.data:
40
runner.next = runner.next.next
41
else:
42
runner = runner.next
43
current = current.next
44
​
45
# How it Works
46
# 1. We define two classes, Node and LinkedList. Node represents an element in the linked list, and LinkedList is used to manage the list.
47
# 2. The `append` method is used to add elements to the linked list.
48
# 3. The `display` method is used to print the elements of the linked list.
49
# 4. The `remove_duplicates` method is used to remove duplicate elements from the linked list.
50
# 5. We iterate through the list, and for each element, we iterate through the rest of the list to remove any duplicates.
51
​
52
# Input/Output
53
if __name__ == "__main__":
54
linked_list = LinkedList()
55
elements = [1, 2, 2, 3, 4, 4, 5]
56
​
57
for element in elements:
58
linked_list.append(element)
59
​
60
print("Original Linked List:")
61
linked_list.display()
62
​
63
linked_list.remove_duplicates()
64
print("\nLinked List after removing duplicates:")
65
linked_list.display()
66
​
How it works
- We define two classes, Node and LinkedList.
- Node represents an element in the linked list, and LinkedList is used to manage the list.
- The `append` method is used to add elements to the linked list.
- The `display` method is used to print the elements of the linked list.
- The `remove_duplicates` method is used to remove duplicate elements from the linked list.
- We iterate through the list, and for each element, we iterate through the rest of the list to remove any duplicates.
Input / Output
