This article explores different ways to prepend an element to a list in Kotlin. In other words, insert an element at the front of the list.

1. Using add() function

The standard solution to prepend an element to the front of a list, you can use the add() function with the specified index as 0.

Download Code

 
You can easily extend the solution to add multiple elements to the front of the list.

Download Code

2. Using LinkedList‘s push() function

If you’re using Doubly linked list implementation of the List and Deque interfaces, say a LinkedList, you can easily prepend elements to it with native push() function. This is demonstrated below:

Download Code

 
This function is equivalent to the addFirst() function.

Download Code

3. Using + operator

Another solution is to wrap the given element in a list and then use the + operator to concatenate the new list with the original list.

Download Code

 
The + operator is equivalent to the plus() function shown below:

Download Code

That’s all about prepending an element to a list in Kotlin.