Remove Duplicates from Linked List

This technical problem deals with removing duplicate elements from a linked list. Given an input list with duplicates, the goal is to produce an output list with all duplicates removed. An example input and output is provided.

Problem

Given a linked list, remove all duplicates from the list.
Example input:
1->2->3->3->4->4->5
Example output:
1->2->3->4->5

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node

    def printList(self):
        temp = self.head
        while (temp):
            print(temp.data)
            temp = temp.next

    def removeDuplicates(self):
        temp = self.head
        if temp is None:
            return
        while temp.next is not None:
            if temp.data == temp.next.data:
                new = temp.next.next
                temp.next = None
                temp.next = new
            else:
                temp = temp.next

if __name__ == '__main__':
    llist = LinkedList()
    llist.push(5)
    llist.push(4 )
    llist.push(4)
    llist.push(3)
    llist.push(3)
    llist.push(2)
    llist.push(1)
    print("Linked List before removing duplicates")
    llist.printList()
    llist.removeDuplicates()
    print("\nLinked List after removing duplicates")
    llist.printList()

A.I. Evaluation of the Solution

The candidate's solution correctly removes duplicates from a linked list. The approach is straightforward and easy to follow.

Evaluated at: 2022-11-06 04:15:58