Equal elements in linked lists

Hey, I am working on a school project and I'm supposed to write a function checking if two doubly linked list contain the same elements. Obviously I could do it with two loops, but that would be 0 points for me. I was also thinking about sorting them and then comparing the same positions of each list, but I'm not sure if there isn't some simplier solution...if it's possible, could you push me to the right direction, please?
I was also thinking about sorting them and then comparing the same positions of each list


If your lists are:

2-3-4-5

and

5-6-7-8

sorting them and comparing the same positions won't find the matching 5.
It seems that I didn't clarify that I just want to know if the lists have all the elements same, so in this case it would just return false or smth and I would be fine.
Compare lengths, reject if different.

If same lengths, then std::sort each, followed by std::mismatch. All in <algorithm>.
Last edited on
Also...it's not what I'm supposed to do, but how could it be done if the lists contained strings? I can think only about comparing every element from the first one with every element from the second.
It doesn't matter what type of data is in the lists - int, double, string, user-defined classes, ...
As long as they can be compared for equality then the same method would work.

The only minor issue with strings is if you allow equality regardless of case; e.g. "UPPER" is equal to "upper". In these cases you would require predicates in any algorithms that you use.
Topic archived. No new replies allowed.