educative.io

Educative

In the given solution for Union, when either list1 or list2 is empty, why is the returned value elements(list2) or elements(list1), and not just list2 or list1?

I’m unable to understand the purpose of elements(list1). Isn’t elements a method for Counter class. What am I missing? Thanks.

Hi @Akshat_Verma

Given two lists, A and B, the union is the list that contains elements or objects that belong to either A, B, or to both.

So, what we are doing is checking whether a list is empty or not. If it’s empty, then we are simply returning the other list, as a union is the collection of both lists. Since we are giving a proper implementation of the linked list, therefore we are not simply giving a solution as list1 or list2.