Published on

Data Structures with Kotlin: Leetcode (P2)

Authors

Continuing the series of articles on Data Structures & Algorithms knowledge in the Kotlin language, we come to exercises on LinkedList, Stack, Queue!

LinkedList, Stack, Queue

Remove Duplicates from Sorted List

Link LeetCode: https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

class ListNode(var `val`: Int, var next: ListNode? = null)
fun deleteDuplicates(head: ListNode?): ListNode? {
    if (head == null) {
        return null
    }

    val dummyNode = ListNode(0)
    dummyNode.next = head
    var currentNode = head
    var prevNode = dummyNode

    while (currentNode != null) {
        if (currentNode.next != null && currentNode.`val` == currentNode.next!!.`val`) {
            while (currentNode?.next != null && currentNode?.`val` == currentNode?.next!!.`val`) {
                currentNode = currentNode.next
            }
            prevNode.next = currentNode
        }

        prevNode = prevNode.next!!
        currentNode = currentNode?.next
    }

    return dummyNode.next
}

Solution guide for Remove Duplicated from Sorted List using Kotlin - Android Mastery

Number of Students Unable to Eat Lunch

Link LeetCode: https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/description

fun countStudents(students: IntArray, sandwiches: IntArray): Int {
    val studentsDeque = ArrayDeque()
    val sandwichesDeque = ArrayDeque()

    // Convert the arrays to deques
    for (student in students) {
        studentsDeque.addLast(student)
    }
    for (sandwich in sandwiches) {
        sandwichesDeque.addLast(sandwich)
    }

    var count = 0

    while (studentsDeque.isNotEmpty()) {
        if (studentsDeque.first() == sandwichesDeque.first()) {
            studentsDeque.removeFirst()
            sandwichesDeque.removeFirst()
            count = 0
        } else {
            count++
            studentsDeque.addLast(studentsDeque.removeFirst())
        }

        if (count == studentsDeque.size) {
            return count
        }
    }

    return 0
}

Solution guide for Number of Students Unable to Earn Lunch using Kotlin DeQueue

Set, Map

Roman to Integer

Link LeetCode: https://leetcode.com/problems/roman-to-integer/description/

fun romanToInt(s: String): Int {
    val m = hashMapOf(
        "I" to 1,
        "IV" to 4,
        "V" to 5,
        "IX" to 9,
        "X" to 10,
        "XL" to 40,
        "L" to 50,
        "XC" to 90,
        "C" to 100,
        "CD" to 400,
        "D" to 500,
        "CM" to 900,
        "M" to 1000
    )
    var res = 0
    var i = 0
    while (i  0) {
            nums[idx] = -nums[idx]
        }
    }
    for (i in nums.indices) {
        if (nums[i] > 0) {
            list.add(i + 1)
        }
    }
    return list
}

Solution guide for Find All Numbers Disappeared in an Array - Android Mastery

Subarray Sum Equals K

LeetCode Link: https://leetcode.com/problems/subarray-sum-equals-k/description/

fun subarraySum(nums: IntArray, k: Int): Int {
    val map = HashMap()
    map[0] = 1
    var count = 0
    var sum = 0
    for (num in nums) {
        sum += num
        count += map[sum-k] ?: 0
        map[sum] = (map[sum] ?: 0) + 1
    }
    return count
}

Solution guide for Sub Array Sum Equals K using Kotlin language - Android Mastery

Above are my solution guides for some simple LeetCode exercises using the most commonly used data structures of the Kotlin programming language. If you need to learn more in-depth, understand more clearly the methods of approaching the problem, analyzing and solving, you can refer to my Android Mastery course. In this course, I will explain everything thoroughly for a person learning Android programming from a Kotlin newbie to an Android Development Master.

I wish you success soon with your learning goals!