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...
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...
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).
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT