Question

Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked listpublic class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraL

Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3
public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3); head.next.next.next-new Node (1); head.next.next.next.next-new Node(2); head = getUnique(head); printLinkedList (head); PROBLEM 2 System.out.println("PROBLEM 2"); string str= "I like queues but queues do not like me"; ArrayList toIgnore new ArrayList); toIgnore.add("stacks"); System.out.println(getMostCommonword(str, toIgnore)); // should return "like" PROBLEM 3 System.out.println("PROBLEM 3"); str-"CSC2720" System.out.printin(findUnique Index(str)); /I Should return 1 str-"toto"; System.out.println(findUniqueIndex(str)); /I Should return -1 public static Node getUnique (Node head) / PROBLEM 1: INSERT CODE HERE return null; public static int findUniqueIndex (String str) // PROBLEM 2: INSERT CODE HERE return 0; public static String getMostCommonword (String str, ArrayList list) // PROBLEM 3: INSERT CODE HERE return null; public static void printLinkedList (Node head) for(Node cur head;cur!-null;cur-cur.next) System.out.print (cur.itemt" "; System.out.println);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Node{
   int item;
   Node next;
  
   Node(int d){
       item = d;
       next = null;
   }
  
}


public class ExtraLab{
  
   public static void main(String[] arg) {
       System.out.println("PROBLEM 1");
      
       Node head = new Node(1);
       head.next = new Node(2);
head.next.next = new Node(3);  
head.next.next.next = new Node(1);  
head.next.next.next.next = new Node(2);  
printLinkedList(head);
head = getUnique(head);
printLinkedList(head);
  

   }

   public static void printLinkedList(Node head) {
       for(Node cur = head; cur != null; cur = cur.next) {
           System.out.println(cur.item+" ");
       }
       System.out.println();
   }

   public static Node getUnique(Node head) {
      
       if(head==null){
   return null;
   }
      
       HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  
   Node currNode = head.next;
   Node prevNode = head;
   Node temp;
   map.put(head.item, 1);
  
   while(currNode!=null){
   int data = currNode.item;
   if(map.containsKey(data)){
   prevNode.next = currNode.next;
   temp= currNode;
   currNode = currNode.next;
   temp.next = null;
   }else{
       map.put(data, 1);
   prevNode = currNode;
   currNode = currNode.next;
   }
}

return head;
}
}

D ExtraLab,java 3 39 public static Node getUnique (Node head) if(head--null) return null; HashMap< Integer, Integer> HashMap<

Add a comment
Know the answer?
Add Answer to:
Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm ...

    Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm We have seen in class how selection sort algorithm works on arrays data structure. In this lab we will practice how selection sort can be performed on a linked list ADT 1. Convert the following selection sort pseudo-code to perform the sort in ascending order (selectionsort asc function) a. Find the node with the minimum value in the linked list of length rn b. Append...

  • Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...

    Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList {    private class Node {        private Object data; //Assume data implemented Comparable        private Node next, prev;        private Node(Object data, Node pref, Node next)        {            this.data = data;       ...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...

    CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list...

  • iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains...

    iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT