"If the facts don't fit the theory, get new facts" --Albert Einstein
Alex Ba wrote:But how would I do that in a method from a class?
"If the facts don't fit the theory, get new facts" --Albert Einstein
When you do things right, people won't be sure you've done anything at all.
"If the facts don't fit the theory, get new facts" --Albert Einstein
Patricia Samuel wrote:It seems a better solution - Using set to check the equality.
Great Tripathi![]()
For my current project, I have to write a method that checks to see if one linked list is equal to another. They must be the same length, but the nodes don't have to be in the same spot.
shivendra tripathi wrote:Add content of one list to Set, store the size of set. Add content of another List, check if Set size gets increased. If so two lists are not equal.
Rahul.p Kumar wrote:Now, create a set of first list, time complexity is (n^2 + n)/2 worst case scenario (i.e. order of two), now when you add second list of same size say n then extra time of order two.
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Mike Simmons wrote:
Rahul.p Kumar wrote:Now, create a set of first list, time complexity is (n^2 + n)/2 worst case scenario (i.e. order of two), now when you add second list of same size say n then extra time of order two.
No, it isn't. Not unless the equals() and hashCode() methods are really, really bad.
Rahul.p Kumar wrote: ok, Vivek, I see flaw in containsAll(), as {1, 2, 3, 4} is not equal to {1, 2, 3, 2}, then probably we should check both way l1.containsAll(l2) and if(true) then check l2.containsAll(l1) further. But now it sounds somewhat less simple and off the shelf
SCJP 1.5(97%) My Blog
Returns true if this list contains all of the elements of the specified collection
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Patricia Samuel wrote:
Rahul - Please be nice on the forum. We are just discussing the topic.
shivendra tripathi wrote:
Rahul.p Kumar wrote: ok, Vivek, I see flaw in containsAll(), as {1, 2, 3, 4} is not equal to {1, 2, 3, 2}, then probably we should check both way l1.containsAll(l2) and if(true) then check l2.containsAll(l1) further. But now it sounds somewhat less simple and off the shelf
even this will not work think abt L1 = {1,2,2,3} L2 = {1,3,3,2}
Rahul.p Kumar wrote:even with well overridden equal() and hashcode(), if you will put first element in set it checks for 0 element, for second element, check is one times, whether it wsi present or not for third element, two times and so on.
Rahul.p Kumar wrote:
shivendra tripathi wrote:
Rahul.p Kumar wrote: ok, Vivek, I see flaw in containsAll(), as {1, 2, 3, 4} is not equal to {1, 2, 3, 2}, then probably we should check both way l1.containsAll(l2) and if(true) then check l2.containsAll(l1) further. But now it sounds somewhat less simple and off the shelf
even this will not work think abt L1 = {1,2,2,3} L2 = {1,3,3,2}
You are right Tripathi.
Mike Simmons wrote:Actually, wait a minute - the code you've shown so far doesn't even seem to work for the last example given, {1,2,2,3} vs. {1,3,3,2}. Maybe you should restate for us: what algorithm are you currently advocating?
grapes are vegan food pellets. Eat this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|