Files

41 lines
573 B
Go
Raw Permalink Normal View History

2025-08-23 23:09:54 +08:00
package main
type ListNode struct {
Val int
Next *ListNode
}
func main() {
}
func isPalindrome(head *ListNode) bool {
numlist := make([]int, 0)
for head != nil {
numlist = append(numlist, head.Val)
head = head.Next
}
if len(numlist) == 1 {
return true
}
left := len(numlist)/2 - 1
right := left + 1
if len(numlist)%2 != 0 {
left = len(numlist) / 2
right = left
} else {
left = len(numlist)/2 - 1
right = left + 1
}
for left >= 0 {
if numlist[left] == numlist[right] {
left--
right++
} else {
return false
}
}
return true
}