Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
func isPalindrome(head *ListNode) bool {
lg := 0
curr := head
st := []int{}
for curr != nil {
lg++
st = append(st, curr.Val)
curr = curr.Next
}
for i := 0; i < lg/2; i++ {
if st[i] != st[lg-1-i] {
return false
}
}
return true
}