def _list_intersection(list1, list2):
    """Compute the list of all elements present in both list1 and list2.
    Duplicates are allowed. Assumes both lists are sorted."""
    intersection = []

    pos1 = 0
    pos2 = 0

    while pos1 < len(list1) and pos2 < len(list2):
        val1 = list1[pos1]
        val2 = list2[pos2]

        if val1 == val2:
            intersection.append(val1)
            pos1 += 1
            pos2 += 1
        elif val1 < val2:
            pos1 += 1
        else:
            pos2 += 1

    return intersection