def union(llist_1, llist_2):
    # edge case, if either of the lists is null, we return the other list
    if llist_1 is None or llist_1.head is None:
        return llist_2
    if llist_2 is None or llist_2.head is None:
        return llist_1

    # the final linked list which we will return
    unioned_list = LinkedList()

    # in union, we need to make sure that elements in list 1 aren't repeated in list 2
    # so we need to use a set to ensure no duplicates
    unioned_set = set()

    # we loop through the first linked list, and add its elements to the set
    current_node = llist_1.head
    while current_node is not None:
        unioned_set.add(current_node.value)
        current_node = current_node.next

    # we loop through the second linked list, and add its elements to the set
    current_node = llist_2.head
    while current_node is not None:
        unioned_set.add(current_node.value)
        current_node = current_node.next

    # now we create the final linked list and return it
    for value in unioned_set:
        unioned_list.append(value)

    return unioned_list
Example #2
0
def intersection(llist_1, llist_2):
    # edge case, if any of the lists is null, we return null
    if llist_1 is None or llist_2 is None:
        return None
    if llist_1.head is None or llist_2.head is None:
        return None
    
    # the final linked list which we will return
    intersected_list = LinkedList()

    # in intersection, we need to store items which appear in both lists
    # so we use a set to store the elements in list 1
    set_1 = set()
    # and another set to store the intersected elements
    intersected_set = set()

    # we loop through the first linked list, and add its elements to set 1
    current_node = llist_1.head
    while current_node is not None:
        set_1.add(current_node.value)
        current_node = current_node.next

    # we loop through the second linked list
    current_node = llist_2.head
    while current_node is not None:
        # if the value of the current node exists in list 1, we add it to our final set
        if current_node.value in set_1:
            intersected_set.add(current_node.value)
        current_node = current_node.next
    
    # now we add the intersected elements to our final linked list and return it
    for value in intersected_set:
        intersected_list.append(value)
    
    # if the intersection is an empty list, we return None
    # otherwise we return the list
    return intersected_list if intersected_list.head is not None else None
from Linked_List import LinkedList
from Union import union
from Intersection import intersection

# Test case 1, 2 linked lists with elements in union and intersection
print("\nChecking TEST_CASE_1: 2 linked lists with elements in union and intersection\n")

linked_list_1 = LinkedList()
linked_list_2 = LinkedList()

element_1 = [3,2,4,35,6,65,6,4,3,21]
element_2 = [6,32,4,9,6,1,11,21,1]

for i in element_1:
    linked_list_1.append(i)

for i in element_2:
    linked_list_2.append(i)


print (union(linked_list_1,linked_list_2))
print (intersection(linked_list_1,linked_list_2))
'''
Checking TEST_CASE_1: 2 linked lists with elements in union and intersection

32 -> 65 -> 2 -> 35 -> 4 -> 6 -> 1 -> 9 -> 11 -> 3 -> 21 -> 
4 -> 21 -> 6 -> 
'''

# Test case 2, 2 linked lists with elements in union but no intersection
print("\nChecking TEST_CASE_2: 2 linked lists with elements in union but no intersection\n")