/**
 * Given two sorted lists, return a new list of the elements that are present in both lists.
 *
 * @param list1 A sorted list of integers
 * @param list2 A sorted list of integers
 * @return A new list containing the elements present in both lists
 *
 * Example:
 * listIntersection(listOf(1, 2, 3, 4, 5), listOf(4, 5, 6, 7, 8)) // [4, 5]
 * listIntersection(listOf(1, 2, 3), listOf(4, 5, 6)) // []
 */
fun listIntersection(list1: List<Int>, list2: List<Int>): List<Int> {

    val intersection = mutableListOf<Int>()
    var index1 = 0
    var index2 = 0

    while (index1 < list1.size && index2 < list2.size) {
        when {
            list1[index1] < list2[index2] -> index1++
            list1[index1] > list2[index2] -> index2++
            else -> {
                intersection.add(list1[index1])
                index1++
                index2++
            }
        }
    }

    return intersection
}
/**
 * Given two sorted lists, return a new list of the elements that are present in both lists.
 *
 * @param list1 A sorted list of integers
 * @param list2 A sorted list of integers
 * @return A new list containing the elements present in both lists
 *
 * Example:
 * listIntersection(listOf(1, 2, 3, 4, 5), listOf(4, 5, 6, 7, 8)) // [4, 5]
 * listIntersection(listOf(1, 2, 3), listOf(4, 5, 6)) // []
 */
fun listIntersection(list1: List<Int>, list2: List<Int>): List<Int> {
/**
 * Given two sorted lists, return a new list of the elements that are present in both lists.
 *
 * @param list1 A sorted list of integers
 * @param list2 A sorted list of integers
 * @return A new list containing the elements present in both lists
 *
 * Example:
 * listIntersection(listOf(1, 2, 3, 4, 5), listOf(4, 5, 6, 7, 8)) // [4, 5]
 * listIntersection(listOf(1, 2, 3), listOf(4, 5, 6)) // []
 */
fun listIntersection(list1: List<Int>, list2: List<Int>): List<Int> {

    val intersection = mutableListOf<Int>()
    var index1 = 0
    var index2 = 0

    while (index1 < list1.size && index2 < list2.size) {
        when {
            list1[index1] < list2[index2] -> index1++
            list1[index1] > list2[index2] -> index2++
            else -> {
                intersection.add(list1[index1])
                index1++
                index2++
            }
        }
    }

    return intersection
}