Skip to content

Commit 72ded41

Browse files
authored
Add Reverse List (#146)
1 parent 6093917 commit 72ded41

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ multiple times and be persistent over time.
6969
- [Capitalize first](src/test/kotlin/com/igorwojda/list/capitalizefirst)
7070
- [Decapitalize const](src/test/kotlin/com/igorwojda/string/decapitalizeconst)
7171
- [Longest word](src/test/kotlin/com/igorwojda/string/longestword)
72+
- [Reverse list](src/test/kotlin/com/igorwojda/list/reverse)
7273

7374
**Intermediate**
7475

misc/ChallengeGroups.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ We use sliding window instead of nested loops which decreases complexity from `O
9898
- [Coins](../src/test/kotlin/com/igorwojda/list/coins)
9999
- [Smallest elements](../src/test/kotlin/com/igorwojda/list/smallestelements)
100100
- [Largest elements](../src/test/kotlin/com/igorwojda/list/largestelements)
101+
- [Reverse list](../src/test/kotlin/com/igorwojda/list/reverse)
101102

102103
## Singly Linked List
103104

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.igorwojda.list.reverse
2+
3+
import org.amshove.kluent.shouldBeEqualTo
4+
import org.junit.jupiter.api.Test
5+
6+
private fun reverse(list: List<String>): List<String> {
7+
TODO("Add your solution here")
8+
}
9+
10+
private class Test {
11+
@Test
12+
fun `reverse empty`() {
13+
val list = listOf<String>()
14+
reverse(list) shouldBeEqualTo listOf<String>()
15+
}
16+
17+
@Test
18+
fun `reverse a`() {
19+
val list = listOf("a")
20+
reverse(list) shouldBeEqualTo listOf("a")
21+
}
22+
23+
@Test
24+
fun `reverse even list`() {
25+
val list = listOf("a", "b")
26+
reverse(list) shouldBeEqualTo listOf("b", "a")
27+
}
28+
29+
@Test
30+
fun `reverse odd list`() {
31+
val list = listOf("a", "b", "c")
32+
reverse(list) shouldBeEqualTo listOf("c", "b", "a")
33+
}
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Chunk
2+
3+
## Instructions
4+
5+
Reverse the given list. Don't use `list.reversed()` method.
6+
7+
[Challenge](Challenge.kt) | [Solution](Solution.kt)
8+
9+
## Examples
10+
11+
```kotlin
12+
reverse(["a", "b"]) // ["b", "a"]
13+
14+
reverse(["a", "b", "c"]) // ["c", "b", "a"]
15+
```
16+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.igorwojda.list.reverse
2+
3+
// Time complexity: O(n)
4+
private object Solution1 {
5+
private fun reverse(list: List<String>): List<String> {
6+
val newList = list.toMutableList()
7+
8+
repeat(newList.size / 2) {
9+
val temp = newList[it]
10+
val other = newList[newList.size - it - 1]
11+
newList[it] = other
12+
newList[newList.size - it - 1] = temp
13+
}
14+
15+
return newList
16+
}
17+
}
18+
19+
private object KtLintWillNotComplain

0 commit comments

Comments
 (0)