In this program, we need to remove the duplicate nodes from the given singly linked list.
List after removing duplicate nodes:
In the above list, node 2 is repeated thrice, and node 1 is repeated twice. Node current will point to head, and index will point to node next to current. Start traversing the list till a duplicate is found that is when current's data is equal to index's data. In the above example, the first duplicate will be found at position 4. Assign current to another node temp. Connect temp's next node with index's next node. Delete index which was pointing to duplicate node. This process will continue until all duplicates are removed.
Create a class
Node which has two data attributes:
next. The data attribute
next points to the next node in the list.
Create another class
LinkedList which has two data attributes:
add_node() method will add a new node to the list:
a. Create a new node.
b. It first checks, whether the
head is equal to null which means the list is empty.
c. If the list is empty, both
tail will point to a newly added node.
d. If the list is not empty, the new node will be added to end of the list such that
tail's next will point to a newly added node. This new node will become the new tail of the list.
remove_duplicate() method will remove duplicate nodes from the list.
a. Define a new node
current which will initially point to head.
temp will point to
index will always point to node next to current.
c. Loop through the list till
current points to null.
d. Check whether current's data is equal to index's data that means
index is duplicate of
e. Since index points to duplicate node so skip it by making node next to temp to will point to node next to index, i.e.
temp.next = index.next.
display() method will display the nodes present in the list:
a. Define a node
current which will initially point to the head of the list.
b. Traverse through the list till
current points to null.
c. Display each node by making
current to point to node next to it in each iteration.
# Represent a node of the singly linked list class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: # Represent the head and tail of the singly linked list def __init__(self): self.head = None self.tail = None # add_node() will add a new node to the list def add_node(self, data): # Create a new node new_node = Node(data) # Checks if the list is empty if self.head == None: # If list is empty, both head and tail will point to new node self.head = new_node self.tail = new_node else: # new_node will be added after tail such that tail's next will point to new_node self.tail.next = new_node # newNode will become new tail of the list self.tail = new_node # removeDuplicate() will remove duplicate nodes from the list def remove_duplicate(self): # Node current will point to head current = self.head index = None temp = None if self.head == None: return else: while current != None: # Node temp will point to previous node to index. temp = current # Index will point to node next to current index = current.next while index != None: # If current node's data is equal to index node's data if current.data == index.data: # Here, index node is pointing to the node which is duplicate of current node # Skips the duplicate node by pointing to next node temp.next = index.next else: # Temp will point to previous node of index. temp = index index = index.next current = current.next # display() will display all the nodes present in the list def display(self): # Node current will point to head current = self.head if self.head == None: print("List is empty") return while current != None: # Increment next to print each node print(current.data) current = current.next def user_input(): slist = LinkedList() # Add data to the list data_list = input("Please enter the elements in the linked list: ").split() for data in data_list: slist.add_node(int(data)) # Display original list print("Original List: ") slist.display() # Remove duplicate nodes (if any) slist.remove_duplicate() print("List after removing duplicate nodes: ") slist.display() if __name__ == "__main__": user_input()
Please enter the elements in the linked list: 1 2 3 2 2 4 1 Original List: 1 2 3 2 2 4 1 List after removing duplicate nodes: 1 2 3 4