> For the complete documentation index, see [llms.txt](https://gold.gitbook.io/kotlin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gold.gitbook.io/kotlin/collections/elements-operations/lastornull.md).

# lastOrNull

```kotlin
public fun <T> List<T>.lastOrNull(): T? {
    return if (isEmpty()) null else this[size - 1]
}

val list = listOf(1, 2, 3, 4, 5)
println(list.lastOrNull())  // 5
println(listOf<Int>().lastOrNull())  // null
```

```kotlin
public inline fun <T> List<T>.lastOrNull(predicate: (T) -> Boolean): T? {
    val iterator = this.listIterator(size)
    while (iterator.hasPrevious()) {
        val element = iterator.previous()
        if (predicate(element)) return element
    }
    return null
}

val list = listOf(1, 2, 3, 4, 5)
println(list.lastOrNull{ it > 3 })  // 5
println(list.lastOrNull{ it > 7 })  // null
```
