u
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user