Question

In: Computer Science

Given a linked list of integers, remove any nodes from the linked list that have values...

Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)

Solutions

Expert Solution

Program in Python:-

class Node:
    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    # This function to initialize head
    def __init__(self):
        self.head = None

    #This function is used to insert a
    # new node at the beginning
    def insert(self, data):
        node = Node(data)
        node.next=self.head
        self.head = node

    #This function to print the LinkedList
    def printList(self):
        current  = self.head
        while current:
            print(current.data,end=" ")
            current= current.next

    #This function is used to removes duplicates
    #elements from the LinkedList
    def removeDuplicates(self):
        temp = ptr = self.head
        while temp is not None:  #Pick elements one by one
            # Compare the picked element with rest of the elements in the LinkedList
            while ptr.next is not None:
                # If duplicate element find then delete it
                if ptr.next.data == temp.data:
                    ptr.next = ptr.next.next
                else:
                    ptr = ptr.next
            temp = temp.next
            ptr = temp


l= LinkedList()
l.insert(10)
l.insert(20)
l.insert(30)
l.insert(20)
l.insert(15)
l.insert(10)
l.insert(20)
l.insert(40)
l.insert(30)


print("Elements of LinkedList before removing duplicate elements: ")
l.printList()

l.removeDuplicates()

print("\nElements of LinkedList after removing duplicate elements: ")
l.printList()

Output:-


Related Solutions

Task 1: Remove Number Complete the function remove number such that given a list of integers...
Task 1: Remove Number Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. Task 2: Logged List The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
Consider a “Remove” operation from a doubly linked list other than front and end. Explain the...
Consider a “Remove” operation from a doubly linked list other than front and end. Explain the implementation of “Remove” operation using a drawing by showing appropriate links. Using the explanation of (a) write the statements to implement the “Remove” operation. Using (b) show that the complexity of “Remove” operation is O(1).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT