Skip to main content

How to remove elements from a LinkedList in Kotlin

How to remove elements from a LinkedList in Kotlin.

Here is a detailed step-by-step tutorial on how to remove elements from a LinkedList in Kotlin.

Step 1: Create a LinkedList

First, we need to create a LinkedList and populate it with some elements. In Kotlin, you can create a LinkedList using the LinkedList class from the java.util package.

import java.util.LinkedList

fun main() {
val linkedList = LinkedList<String>()
linkedList.add("Apple")
linkedList.add("Banana")
linkedList.add("Cherry")
linkedList.add("Durian")

// Print the initial LinkedList
println("Initial LinkedList: $linkedList")
}

In the above code, we create a LinkedList and add four elements to it: "Apple", "Banana", "Cherry", and "Durian". We also print the initial LinkedList to verify the elements.

Step 2: Remove elements by value

To remove an element from a LinkedList based on its value, you can use the remove method. It removes the first occurrence of the specified element from the LinkedList.

fun main() {
// ...

// Remove an element by value
val elementToRemove = "Banana"
linkedList.remove(elementToRemove)

// Print the updated LinkedList
println("LinkedList after removing $elementToRemove: $linkedList")
}

In the above code, we remove the element "Banana" from the LinkedList using the remove method. After removing the element, we print the updated LinkedList to verify the removal.

Step 3: Remove elements by index

To remove an element from a LinkedList based on its index, you can use the removeAt method. It removes the element at the specified index from the LinkedList.

fun main() {
// ...

// Remove an element by index
val indexToRemove = 2
linkedList.removeAt(indexToRemove)

// Print the updated LinkedList
println("LinkedList after removing element at index $indexToRemove: $linkedList")
}

In the above code, we remove the element at index 2 from the LinkedList using the removeAt method. After removing the element, we print the updated LinkedList to verify the removal.

Step 4: Remove elements using iterator

To remove elements from a LinkedList while iterating over it, you can use an iterator and the remove method of the iterator. It removes the current element from the LinkedList during iteration.

fun main() {
// ...

// Remove elements using iterator
val iterator = linkedList.iterator()
while (iterator.hasNext()) {
val element = iterator.next()
if (element.startsWith("A")) {
iterator.remove()
}
}

// Print the updated LinkedList
println("LinkedList after removing elements starting with 'A': $linkedList")
}

In the above code, we remove elements from the LinkedList that start with the letter "A" using an iterator. We iterate over the LinkedList using a while loop and check each element. If an element starts with "A", we remove it using the iterator's remove method. After removing the elements, we print the updated LinkedList to verify the removal.

That's it! You now know how to remove elements from a LinkedList in Kotlin.