/**
 * Given two lists of integers, return a new list containing the intersection of the two lists,
 * sorted in ascending order without any duplicates.
 *
 * @param list1 A list of integers (sorted in ascending order).
 * @param list2 A list of integers (sorted in ascending order).
 * @return A list containing the intersection of list1 and list2, sorted in ascending order without duplicates.
 */
fun mergeLists(list1: List<Int>, list2: List<Int>): List<Int> {

    val intersection = mutableListOf<Int>()
    var p1 = 0
    var p2 = 0

    while (p1 < list1.size && p2 < list2.size) {
        when {
            list1[p1] < list2[p2] -> p1++
            list1[p1] > list2[p2] -> p2++
            else -> {
                if (intersection.isEmpty() || intersection.last() != list1[p1]) {
                    intersection.add(list1[p1])
                }
                p1++
                p2++
            }
        }
    }

    return intersection
}
/**
 * Given two lists of integers, return a new list containing the intersection of the two lists,
 * sorted in ascending order without any duplicates.
 *
 * @param list1 A list of integers (sorted in ascending order).
 * @param list2 A list of integers (sorted in ascending order).
 * @return A list containing the intersection of list1 and list2, sorted in ascending order without duplicates.
 */
fun mergeLists(list1: List<Int>, list2: List<Int>): List<Int> {
/**
 * Given two lists of integers, return a new list containing the intersection of the two lists,
 * sorted in ascending order without any duplicates.
 *
 * @param list1 A list of integers (sorted in ascending order).
 * @param list2 A list of integers (sorted in ascending order).
 * @return A list containing the intersection of list1 and list2, sorted in ascending order without duplicates.
 */
fun mergeLists(list1: List<Int>, list2: List<Int>): List<Int> {

    val intersection = mutableListOf<Int>()
    var p1 = 0
    var p2 = 0

    while (p1 < list1.size && p2 < list2.size) {
        when {
            list1[p1] < list2[p2] -> p1++
            list1[p1] > list2[p2] -> p2++
            else -> {
                if (intersection.isEmpty() || intersection.last() != list1[p1]) {
                    intersection.add(list1[p1])
                }
                p1++
                p2++
            }
        }
    }

    return intersection
}