Learn Programming and technology with ease @ developerpublish.com

HomePythonPython Program to Remove Duplicates from a Linked List

# Python Program to Remove Duplicates from a Linked List

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

def __init__(self):

def append(self, data):
new_node = Node(data)
return
while current.next:
current = current.next
current.next = new_node

def display(self):
while current:
print(current.data, end=' ')
current = current.next
print()

def remove_duplicates(self):
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__":
elements = [1, 2, 2, 3, 4, 4, 5]

for element in elements:

```

## How it works

1. We define two classes, Node and LinkedList.
2. Node represents an element in the linked list, and LinkedList is used to manage the list.
3. The `append` method is used to add elements to the linked list.
4. The `display` method is used to print the elements of the linked list.
5. The `remove_duplicates` method is used to remove duplicate elements from the linked list.
6. We iterate through the list, and for each element, we iterate through the rest of the list to remove any duplicates.

## Input / Output

### You May Also Like

#### Python Program to Solve the Celebrity Problem

This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...

#### Python Program to Solve n-Queen Problem with Recursion

This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...

#### Python Program to Find Intersection and Union of Two Linked Lists

In this Python program, we will be implementing the intersection and union operations on two linked lists. Linked lists are...