How to Remove Duplicates from a Linked List in Python

In this tutorial, we will learn how to program "How to Remove Duplicates from a Linked List in Python." The objective is to remove duplicates from a linked list. This tutorial will guide you step by step through the process of managing a linked list. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and improve your coding skills.

This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of removing duplicates from a linked list. So, let's dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. class Node:
  2.     def __init__(self, data):
  3.         self.data = data
  4.         self.next = None
  5.  
  6. class LinkedList:
  7.     def __init__(self):
  8.         self.head = None
  9.         self.last_node = None
  10.  
  11.     def append(self, data):
  12.         if self.last_node is None:
  13.             self.head = Node(data)
  14.             self.last_node = self.head
  15.         else:
  16.             self.last_node.next = Node(data)
  17.             self.last_node = self.last_node.next
  18.  
  19.     def get_prev_node(self, ref_node):
  20.         current = self.head
  21.         while current and current.next != ref_node:
  22.             current = current.next
  23.         return current
  24.  
  25.     def remove(self, node):
  26.         prev_node = self.get_prev_node(node)
  27.         if prev_node is None:
  28.             self.head = self.head.next
  29.         else:
  30.             prev_node.next = node.next
  31.  
  32.     def display(self):
  33.         current = self.head
  34.         while current:
  35.             print(current.data, end=' ')
  36.             current = current.next
  37.         print()  # newline
  38.  
  39. def remove_duplicates(llist):
  40.     current1 = llist.head
  41.     while current1:
  42.         data = current1.data
  43.         current2 = current1.next
  44.         while current2:
  45.             next_node = current2.next  # save next node
  46.             if current2.data == data:
  47.                 llist.remove(current2)
  48.             current2 = next_node
  49.         current1 = current1.next
  50.  
  51. while True:
  52.     print("\n============= Remove Duplicates from a Linked List =============\n")
  53.    
  54.     a_llist = LinkedList()
  55.  
  56.     data_list = input('Please enter the elements in the linked list (space-separated): ').split()
  57.     for data in data_list:
  58.         a_llist.append(int(data))
  59.  
  60.     remove_duplicates(a_llist)
  61.  
  62.     print('The list with duplicates removed:')
  63.     a_llist.display()
  64.  
  65.     # Ask user if they want to try again
  66.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  67.     if opt == 'no':
  68.         print("Exiting program...")
  69.         break
  70.     elif opt != 'yes':
  71.         print("Invalid choice. Exiting program...")
  72.         break
This program removes duplicate values from a singly linked list. Users can input elements to create a linked list, and the program traverses the list to eliminate any repeated values while preserving the original order. The resulting list, containing only unique elements, is then displayed. After completion, users can choose to run the program again or exit.

Output:

There you have it we successfully created How to Remove Duplicates from a Linked List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials