Reverse + ReverseInPlace

This commit is contained in:
2025-10-24 20:30:44 +02:00
parent 385dad461c
commit 6b0395e54a

View File

@ -1,6 +1,15 @@
package slices
func Reverse[S ~[]T, T any](s S) {
func Reverse[T any](slice []T) []T {
s := make([]T, len(slice))
for i := range slice {
ri := len(slice) - 1 - i
s[ri] = slice[i]
}
return s
}
func ReverseInPlace[S ~[]T, T any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}