shivendra tripathi wrote:Vivek your code won't work. It will give false even for {1,2,2,3} vs. {1,2,2,3}.
Yes you are right..
Rahul.p Kumar wrote: then best solution I can think of is use hashmap with key as unique element of corresponding list and value as no of repeating of that element. Compare two keysets for size first, if fail then fail all round, if true compare values of each key [sigh]
Repeat for each Linked List
1. Convert your linked list into an array by using <listObject>.toArray() method which returns an Object[]
2. Sort the array using Arrays.sort(Object[]) with the array you have obtained on step #1
After getting the sorted array equivalent of each linked list,
3. Invoke Arrays.equals(Object[] array1, Object[] array2) to get to know whether they are equal or not.
Note: The sorting is done based on the natural ordering of elements.
I guess for your linked list of numbers, it would be fine. (so long as it is of built in types like int, float, String etc., as they have their own ordering well defined in the corresponding classes).
As I am unable to provide the URL for the methods besides each method here, I am pasting those FYI here.
I'm not really sure why everyone brought up sets, and hashs, and the containsAll() method. But my impression from Alex's first post was that he was supposed to CREATE a method to compare the two lists. When the word create usually appears in my assignments it means don't use built in methods, try to do it yourself. I still think the original while loop solution will work, it might take a little while, but it should work.
-Hunter
"If the facts don't fit the theory, get new facts" --Albert Einstein
Hunter McMillen wrote:I'm not really sure why everyone brought up sets, and hashs, and the containsAll() method. But my impression from Alex's first post was that he was supposed to CREATE a method to compare the two lists. When the word create usually appears in my assignments it means don't use built in methods, try to do it yourself. I still think the original while loop solution will work, it might take a little while, but it should work.
-Hunter
Yes indeed. I too mentioned the same in my very first post!