Question

Q3: Write the method that returns true if all the elements of the linked list are...

Q3: Write the method that returns true if all the elements of the linked list are different. It return false otherwise

(in java + the output pic )

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListUnique {

    public static <E> boolean linkedListDifferent(LinkedList<E> list) {
        for (int i = 0; i < list.size(); i++) {
            for (int j = i+1; j < list.size(); j++) {
                if (list.get(i).equals(list.get(j))) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>(Arrays.asList(2, 8, 1, 5, 1));
        System.out.println(list + " : " + linkedListDifferent(list));

        LinkedList<Integer> list1 = new LinkedList<>(Arrays.asList(2, 8, 1, 5, 7));
        System.out.println(list1 + " : " + linkedListDifferent(list1));
    }
}

Add a comment
Know the answer?
Add Answer to:
Q3: Write the method that returns true if all the elements of the linked list are...
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
  • Assume you have a linked list of integer data pointed by head (p-head). head e2 next...

    Assume you have a linked list of integer data pointed by head (p-head). head e2 next nextー next next Qu: Write the recursive method that duplicates (make a separate copy) the linked list (2 Points) Q2: Write the method that sort the linked list in increasing order (from smallest to the largest) (2 Points) Q3: Write the method that returns true if all the elements of the linked list are different. It return false otherwise (1 Point)

  • Write a c++ routine that returns true if two elements in a singly linked list of...

    Write a c++ routine that returns true if two elements in a singly linked list of integers add up to a given target (similar to the two sum problem 1). Your function should return true and print the individual numbers, or return false. Examples: - If your list is: 1 → 3 → 4 → 14 → 5 and your target is 8, then you will return true and print: 3, 5 (since 3 + 5 = 8). - If...

  • Write a recursive method contains(int target, LLNode<Integer> list) that returns true if list contains target and...

    Write a recursive method contains(int target, LLNode<Integer> list) that returns true if list contains target and false otherwise. For our example list contains(15, values) would return true while contains(10, values) would return false. (JAVA language)

  • 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; }...

  • Write a method booleanisValid() that takes a list of 3 boolean values as argument and returns...

    Write a method booleanisValid() that takes a list of 3 boolean values as argument and returns true if any of the entries in the array is true, and false otherwise. This can be done in 4 lines (2 of the lines are { and }   )               Fill in the Solution:                              public static boolean isValid(______ _______, ______ _______, ______ _______)                              {                                 return(true);                              } Java language, thank you.

  • Write a method max() that takes a reference to the first node in a linked list...

    Write a method max() that takes a reference to the first node in a linked list as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty. In java

  • Write a public method “getSum()” that returns the sum of all elements in binary tree. [Please...

    Write a public method “getSum()” that returns the sum of all elements in binary tree. [Please check that the data are integers, otherwise throws exception]. Note: you should use internal recursive private method to print all paths. java

  • Java question Write the method reversed that returns true if and only if the arrays a...

    Java question Write the method reversed that returns true if and only if the arrays a and b contain exactly the same elements, but in reversed order. For example, reversed ({3, 1}, {1, 3}) returns true, but reversed ({3, 1}, {2, 3}) and reversed ({3, 1}, {1, 1, 3}) both return false. public static boolean reversed (int [] a, int [] b) should return true if and only if a and b contain the same elements, reversed.

  • 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...

    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);...

  • *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked...

    *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked list recursively . The method returns a string with every element in the list and a space in between each element in reverse order. THE METHOD DOES NOT REVERSE THE LINKED LIST. IT JUST RETURNS A STRING CONTAINING THE ELEMENTS OF THE LIST IN REVERSE ORDER. ASSUME ALL THE ELEMENTS IN THE LINKED LIST ARE int... method signature is something like : public String...

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