Files
leetcode/top-100-liked/24/24_test.go
T
2026-06-13 04:08:10 +10:00

45 lines
687 B
Go

package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/palindrome-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
type ListNode struct {
Val int
Next *ListNode
}
func isPalindrome(head *ListNode) bool {
vals := []int{}
for ; head != nil; head = head.Next {
vals = append(vals, head.Val)
}
n := len(vals)
for i, v := range vals[:n/2] {
if v != vals[n-1-i] {
return false
}
}
return true
}
func TestS24(t *testing.T) {
input := ListNode{
Val: 1,
Next: &ListNode{
Val: 2,
Next: &ListNode{
Val: 2,
Next: &ListNode{
Val: 1,
Next: nil,
},
},
},
}
fmt.Println(isPalindrome(&input))
}