This article explores different ways to add values at the beginning of a List in Kotlin.

1. Using add() function

The standard solution to insert a specified element at the specified position in the list is using the add() function. It accepts the index of the element and the element to be inserted, as shown below:

Download Code

2. Using Deque

The add() function creates space for the new element at the beginning, leading to shifting all the list elements to the right (and possible re-allocation of memory). To insert the specified element at the front, consider using the ArrayDeque or LinkedList class, which implements a Deque.

Download Code

3. Using plus() function

A simple solution to combine two lists in Kotlin is using the + operator. The idea is to convert the given values into a list and concatenate it with the given list using the + operator. You may also use the plus() function.

Download Code

4. Using reverse() function

Although not recommended, you can use the reverse() function to reverse the list. Then insert the specified value at its end and reverse the list again to get the desired order.

Download Code

That’s all about adding values at the beginning of a List in Kotlin.