From c0f9c9fd5d718bab9a84fd29245c0803a6912573 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Fri, 17 Feb 2017 12:44:06 +0300 Subject: [PATCH] Implement reverse of lists, fixes few tests. --- .../main/kotlin/kotlin/collections/Collections.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/collections/Collections.kt b/runtime/src/main/kotlin/kotlin/collections/Collections.kt index 8a2c2200e05..249f6e8f2cb 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Collections.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Collections.kt @@ -958,10 +958,18 @@ public inline fun Iterable.takeWhile(predicate: (T) -> Boolean): List /** * Reverses elements in the list in-place. */ -@Fixme public fun MutableList.reverse(): Unit { - //java.util.Collections.reverse(this) - TODO() + val median = size / 2 + var leftIndex = 0 + var rightIndex = size - 1 + while (leftIndex < median) { + val tmp = this[leftIndex] + this[leftIndex] = this[rightIndex] + this[rightIndex] = tmp + leftIndex++ + rightIndex-- + } + } /**